-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDepthBinaryTree_111.java
More file actions
125 lines (101 loc) · 3.53 KB
/
MinimumDepthBinaryTree_111.java
File metadata and controls
125 lines (101 loc) · 3.53 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* ----------------------------------------------------------------------------
Minimum Depth of Binary Tree
- 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.
* ----------------------------------------------------------------------------
*/
/**
* Related: 104 Maximum Depth of Binary Tree
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* 1. Recursion
* - Min length is different from Max length because we should make sure
* the path is ended with a $$leaf$$
*
* 0
* 1 2
* 3
*
* - In the above example, we should not stop at node2 since it has right child
*/
public class Solution {
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;
}
}
//------------------------------------------------------------------------------
/**
* 2. Postorder
* - Preorder & Inorder only push the left tree into stack
* - the depth of the stack is the length of the path
* - only count path that are from root to each leaf node
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode curr = root, lastvisit = null;
int minlength = Integer.MAX_VALUE;
while(curr != null || !stack.isEmpty()) {
if (curr!= null) {
stack.push(curr); lastvisit = curr;
if (curr.left != null) { curr = curr.left; continue; }
else { curr = curr.right; continue; }
}
curr = stack.pop();
//XXXX must also need curr.left == null (single left tree)
if (curr.right == null && curr.left == null) {
minlength = Math.min(minlength, stack.size() + 1); }
if (curr.right == null || curr.right == lastvisit) {
lastvisit = curr; curr = null; }
else {
stack.push(curr); // push it back
lastvisit = curr;
curr = curr.right;
}
}
return minlength;
}
}
//------------------------------------------------------------------------------
/**
* 3. BFS
* - The more efficient way is using bread first search
* - Break early when reaching a leaf instead of traversing the entire tree
* - How to know which floor it is,
* XXXX Use a rightmost
*/
public class Solution{
public int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
TreeNode rightmost = root, curr = root;
int minlength = 1;
while(!queue.isEmpty()) {
curr = queue.poll();
if (curr.left == null && curr.right == null) break;
if (curr == rightmost) {
rightmost = (curr.right == null) ? curr.left : curr.right;
minlength++;
}
if (curr.left != null) queue.offer(curr.left);
if (curr.right!= null) queue.offer(curr.right);
}
return minlength;
}
}