-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostOrder.java
More file actions
86 lines (78 loc) · 2.13 KB
/
Copy pathPostOrder.java
File metadata and controls
86 lines (78 loc) · 2.13 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
import java.util.*;
public class PostOrder{
public static void main(String[] args){
int[] inputArr = {5,3,7,1,2,6,8,10,4,12,13};
Tree tree = new Tree(inputArr[0]);
for(int i=1;i<inputArr.length; i++){
tree.insert(inputArr[i]);
}
System.out.println("===recursion===");
tree.postOrder();
System.out.println("");
System.out.println("===iteration===");
tree.postOrderIteration();
}
}
class Tree{
public Node root;
public Tree(int val){
root = new Node(val);
}
public void insert(int val){
insert(root, val);
}
public void insert(Node node, int val){
if(node == null) return;
if(val <= node.val){
if(node.left == null)
node.left = new Node(val);
else
insert(node.left, val);
}else{
if(node.right == null)
node.right = new Node(val);
else
insert(node.right, val);
}
}
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if(node == null) return;
postOrder(node.left);
postOrder(node.right);
System.out.println(node.val);
}
/* the solution you need */
public void postOrderIteration(){
Stack<Node> leftStk = new Stack<>();
Stack<Node> rightStk = new Stack<>();
rightStk.push(root);
Node cur = root;
while(cur.left != null || cur.right != null || !leftStk.empty()){
if(cur.right != null){
rightStk.push(cur.right);
if(cur.left != null)
leftStk.push(cur.left);
}else{
if(cur.left != null)
rightStk.push(cur.left);
else
rightStk.push(leftStk.pop());
}
cur = rightStk.peek();
}
while(!rightStk.empty()){
System.out.println(rightStk.pop().val);
}
}
}
class Node {
int val;
Node left;
Node right;
public Node(int val){
this.val = val;
}
}