-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumDepthOfBinaryTree.java
More file actions
40 lines (28 loc) · 1.08 KB
/
Copy pathMinimumDepthOfBinaryTree.java
File metadata and controls
40 lines (28 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Given a binary tree, find its minimum depth.
// See: https://leetcode.com/problems/minimum-depth-of-binary-tree/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import leetcode.util.tree.TreeNode;
public class MinimumDepthOfBinaryTree {
public int minDepth(TreeNode root) {
return helper(root, 0);
}
private int helper(TreeNode root, int depth) {
if (root == null) {
return depth;
}
if (root.left == null) {
return helper(root.right, depth + 1);
} else if (root.right == null) {
return helper(root.left, depth + 1);
}
return Math.min(helper(root.left, depth + 1), helper(root.right, depth + 1));
}
public static void main(String[] args) {
MinimumDepthOfBinaryTree sln = new MinimumDepthOfBinaryTree();
TreeNode t1 = initTree(3, 9, 20, null, null, 15, 7);
TreeNode t2 = initTree(1, 2);
System.out.println(sln.minDepth(t1)); // 2
System.out.println(sln.minDepth(t2)); // 2
}
}