-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseLevelTraversal.java
74 lines (62 loc) · 2.18 KB
/
reverseLevelTraversal.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
/**
This algorithm is to implement reverse level traversal of tree, unlike level order traversal, which follows up-to-down manner,
this does bottom-up manner. So I use one stack and one queue.
First, offer root into queue, then poll it into stack, if it has right child, offer into queue, then come left child.
Then, we follow the same procedure as above until queue is empty.
Finally, we take out all nodes from stack.
The example tree is as following: using this algorithm, we get the expected result: -1 10 7 8 6 9 4 5 2 3 1
1
/ \
2 3
/ \ / \
6 9 4 5
\ / \
10 7 8
/
-1
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Solution solution = new Solution();
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(6);
root.left.left.right = new TreeNode(10);
root.left.left.right.left = new TreeNode(-1);
root.left.right = new TreeNode(9);
root.right = new TreeNode(3);
root.right.right = new TreeNode(5);
root.right.left = new TreeNode(4);
root.right.left.left = new TreeNode(7);
root.right.left.right = new TreeNode(8);
solution.stackList(root);
for (int i = 0 ; i < solution.list.size(); i++) System.out.print(solution.list.get(i) + " ");
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
Stack<TreeNode> s = new Stack<>();
Queue<TreeNode> q = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
public void reverseLevelTraversal(TreeNode root) {
if (root == null) return;
q.offer(root);
while(!q.isEmpty()) {
TreeNode tmp = q.poll();
s.push(tmp);
if (tmp.right != null) q.offer(tmp.right);
if (tmp.left != null) q.offer(tmp.left);
}
}
public void stackList(TreeNode root) {
reverseLevelTraversal(root);
while (!s.isEmpty()) list.add(s.pop().val);
}
}