111. Minimum Depth of Binary Tree
和求maxDepth基本一样
public int minDepth(TreeNode root) {
if(root == null) return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
if (left == 0 || right == 0) return left + right + 1;
return Math.min(left, right) + 1;
}
保证树左右子树任意一边为null的时候,返回有高度的一边
if (left == 0 || right == 0) return left + right + 1;
相当于
if (root.left == null || root.right == null) {
return (root.right == null) ? 1 + left : 1 + right;
}