111.Minimum Depth of Binary Tree

Solution 1: accepted 1ms

DFS.

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