|
5 | 5 | import java.util.LinkedList;
|
6 | 6 | import java.util.Queue;
|
7 | 7 |
|
8 |
| -/**Given a binary tree, find its minimum depth. |
9 |
| -
|
10 |
| - The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.*/ |
| 8 | +/** |
| 9 | + * 111. Minimum Depth of Binary Tree |
| 10 | + * Given a binary tree, find its minimum depth. |
| 11 | + * The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. |
| 12 | + * */ |
11 | 13 |
|
12 | 14 | public class _111 {
|
13 |
| -/**We can solve this problem using both BFS and DFS: |
14 |
| - * DFS is to visit every single root to leaf path and return the shortest one. |
15 |
| - * BFS is to visit every level and return whenever we find the first leaf node.*/ |
16 |
| - public int minDepth(TreeNode root) { |
17 |
| - if(root == null) return 0; |
18 |
| - int left = minDepth(root.left); |
19 |
| - int right = minDepth(root.right); |
20 |
| - if(left == 0) return right+1; |
21 |
| - if(right == 0) return left+1; |
22 |
| - return Math.min(left, right)+1; |
23 |
| - } |
24 |
| - |
25 |
| - public static void main(String[] args){ |
26 |
| - _111 test = new _111(); |
27 |
| - TreeNode root = new TreeNode(1); |
28 |
| - root.left = new TreeNode(2); |
29 |
| - root.right = new TreeNode(3); |
30 |
| - System.out.println(test.minDepth(root)); |
| 15 | + /** |
| 16 | + * We can solve this problem using both BFS and DFS: |
| 17 | + * DFS is to visit every single root to leaf path and return the shortest one. |
| 18 | + * BFS is to visit every level and return whenever we find the first leaf node. |
| 19 | + */ |
| 20 | + |
| 21 | + public static class DFSSolution { |
| 22 | + |
| 23 | + public int minDepth(TreeNode root) { |
| 24 | + if (root == null) return 0; |
| 25 | + int left = minDepth(root.left); |
| 26 | + int right = minDepth(root.right); |
| 27 | + if (left == 0) return right + 1; |
| 28 | + if (right == 0) return left + 1; |
| 29 | + return Math.min(left, right) + 1; |
| 30 | + } |
| 31 | + |
31 | 32 | }
|
32 |
| - |
33 |
| - |
34 |
| - public int minDepth_BFS(TreeNode root) { |
35 |
| - if(root == null) return 0; |
36 |
| - Queue<TreeNode> q = new LinkedList(); |
37 |
| - q.offer(root); |
38 |
| - int level = 0; |
39 |
| - while(!q.isEmpty()){ |
40 |
| - level++; |
41 |
| - int size = q.size(); |
42 |
| - for(int i = 0; i < size; i++){ |
43 |
| - TreeNode curr = q.poll(); |
44 |
| - if(curr.left != null) q.offer(curr.left); |
45 |
| - if(curr.right != null) q.offer(curr.right); |
46 |
| - if(curr.left == null && curr.right == null) return level; |
| 33 | + |
| 34 | + public static class BFSSolution { |
| 35 | + |
| 36 | + public int minDepth_BFS(TreeNode root) { |
| 37 | + if (root == null) return 0; |
| 38 | + Queue<TreeNode> q = new LinkedList(); |
| 39 | + q.offer(root); |
| 40 | + int level = 0; |
| 41 | + while (!q.isEmpty()) { |
| 42 | + level++; |
| 43 | + int size = q.size(); |
| 44 | + for (int i = 0; i < size; i++) { |
| 45 | + TreeNode curr = q.poll(); |
| 46 | + if (curr.left != null) q.offer(curr.left); |
| 47 | + if (curr.right != null) q.offer(curr.right); |
| 48 | + if (curr.left == null && curr.right == null) return level; |
| 49 | + } |
47 | 50 | }
|
| 51 | + return level; |
48 | 52 | }
|
49 |
| - return level; |
50 | 53 | }
|
51 |
| - |
52 | 54 | }
|
0 commit comments