-
Notifications
You must be signed in to change notification settings - Fork 118
/
PostOrder.java
94 lines (72 loc) · 2.57 KB
/
PostOrder.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
88
89
90
91
92
93
94
package Algorithms;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
public class PostOrder {
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (root == null) {
return result;
}
Stack<TreeNode> s = new Stack<TreeNode>();
Stack<Integer> s2 = new Stack<Integer>();
int currval = 0;
//s.push(root);
//s2.push(root.val);
TreeNode curr = root;
while (true) {
// store all the right node and the root node. then shift to the left node.
while (curr != null) {
if (curr.right != null) {
// s.push(curr.right);
}
s.push(curr);
s2.push(curr.val);
curr = curr.left;
}
if (s.isEmpty()) {
return result;
}
curr = s.pop();
currval = s2.pop();
if (curr.right != null && (s.isEmpty() || s.peek() != null)) {
// this is to indicate that this node has been visited,
// when come back from the right branch, we can know that we don't need
// to search right branch again.
s.push(null);
s.push(curr);
s2.push(0);
s2.push(curr.val);
curr = curr.right;
} else {
result.add(curr.val);
if (!s.isEmpty() && s.peek() == null) {
s.pop(); // give up the "null" node.
s2.pop();
}
curr = null;
}
}
}
public static void main(String[] args) {
PostOrder p = new PostOrder();
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.right.right = new TreeNode(6);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.left.left.left = new TreeNode(7);
ArrayList<Integer> rst = p.postorderTraversal(root);
System.out.printf(rst.toString());
}
}