111 二叉树的最小深度

给定一个二叉树,找出其最小深度。

示例:

1
2
3
4
5
6
7
8
给定二叉树 [3,9,20,null,null,15,7],

3
/
9 20
/
15 7
返回它的最小深度 2.

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
if(root.left == null){
return minDepth(root.right)+1;
}
if(root.right == null){
return minDepth(root.left) + 1;
}
return Math.min(minDepth(root.left),minDepth(root.right)) + 1;
}