Repository navigation
Expand file tree
/
Copy pathOrderTraversal2.java
More file actions
61 lines (58 loc) · 1.91 KB
/
Copy pathOrderTraversal2.java
File metadata and controls
61 lines (58 loc) · 1.91 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
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class OrderTraversal2 {
// use queue to do BFS and a stack to store the node along the BFS.
// After each level BFS, insert a dummy node to deliminate the level
// You can use ArrayList to add in the front
private void search(LinkedList<TreeNode> q, Stack<Integer> s) {
while (q.size() > 1) {
TreeNode node = q.poll();
s.push(node.val);
if (node.val == Integer.MIN_VALUE) {
q.add(new TreeNode(Integer.MIN_VALUE));
continue;
}
if (node.left == null && node.right == null) // leaf node
continue;
if (node.right != null) {
q.add(node.right);
//s.push(node.right.val);
}
if (node.left != null) {
q.add(node.left);
//s.push(node.left.val);
}
}
}
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null)
return new LinkedList<List<Integer>>();
LinkedList<TreeNode> q = new LinkedList<TreeNode>();
Stack<Integer> s = new Stack<Integer>();
List<List<Integer>> result = new LinkedList<List<Integer>>();
q.add(root);
q.add(new TreeNode(Integer.MIN_VALUE));
search(q, s);
List<Integer> list = new LinkedList<Integer>();
while (!s.isEmpty()) {
int val = s.pop();
if (val == Integer.MIN_VALUE) {
if (!list.isEmpty())
result.add(list);
list = new LinkedList<Integer>();
continue;
}
list.add(val);
}
if (!list.isEmpty())
result.add(list);
return result;
}
}