解题思路:
递归
提交代码:
class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
if(root.left==null&&root.right==null) return 1;
if(root.left==null) return 1+minDepth(root.right);
if(root.right==null) return 1+minDepth(root.left);
int left=minDepth(root.left);
int right=minDepth(root.right);
return Math.min(left, right)+1;
}
}
运行结果: