Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

111. Minimum Depth of Binary Tree #46

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

different from maximum depth. if a node only has one child, the depth will be 1 + child depth.

public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null) return minDepth(root.right) + 1; if(root.right == null) return minDepth(root.left) + 1; int lres = minDepth(root.left) + 1; int rres = minDepth(root.right) + 1; return lres < rres ? lres : rres; } }