class Solution {
public int minDepth(TreeNode root) {
if (root == null){
return 0;
}
int left = minDepth(root.left);
int right = minDepth(root.right);
/**
* 如果左右孩子有一个没有,那就只计算另外一个子树
*/
if (left == 0) {
return right + 1;
}
if (right == 0) {
return left + 1;
}
return Math.min(left, right) + 1;
}
}
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/