111. Minimum Depth of Binary Tree
左右子树选短的,但是注意如果没有子树不能选0,一开始做错一次。。
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
return helper(root, 0);
}
public int helper(TreeNode root, int depth) {
if (root == null) return depth;
if (root.left == null && root.right == null) return depth + 1;
else if (root.left == null) return helper(root.right, depth + 1);
else if (root.right == null) return helper(root.left, depth + 1);
else return Math.min(helper(root.left, depth + 1), helper(root.right, depth + 1));
}
}

浙公网安备 33010602011771号