-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path111. Minimum Depth of Binary Tree.java
executable file
·87 lines (67 loc) · 2.04 KB
/
111. Minimum Depth of Binary Tree.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
E
tags: Tree, DFS, BFS
time: O(n)
space: O(n)
#### BFS
- Shortest path; minimum depth: 想到BFS, check level by level, BFS更能确保更快找到结果
- depth definition: reach to a leaf node, where node.left == null && node.right == null
- BFS using queue, track level.
#### DFS
- Divide and Conquer to find min depth.
- if one of child is null, return the other child depth + 1
- Pick the min of the two child depth + 1
- need to visit all nodes
```
/*
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path
from the root node down to the nearest leaf node.
Example
Given a binary tree as follow:
1
/ \
2 3
/ \
4 5
The minimum depth is 2
Tags Expand
Depth First Search
*/
/*
BFS, 99.67%
minimum depth, consider BFS, it reaches the termination point faster:
if any node has null left/right child, that indicates the first leaf
*/
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int level = 0;
while (!queue.isEmpty()) {
level++;
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.left == null && node.right == null) return level;
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return level;
}
}
// DFS
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left == null && root.right == null) return 1;
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
if (root.left == null || root.right == null) {
return (root.left == null ? rightDepth : leftDepth) + 1;
}
return Math.min(leftDepth, rightDepth) + 1;
}
}
```