二叉树最小深度
递归计算,注意递归的跳出条件
class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
int getMinDepth(TreeNode root) {
if (root == null) {
return 0;
}
return getMin(root);
}
int getMin(TreeNode root) {
if (root == null){
return Math.MAX_VALUE
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.min(getMin(root.left), getMin(root.right)) + 1;
}