-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
PostOrderTraversalTree.java
58 lines (49 loc) · 1.48 KB
/
PostOrderTraversalTree.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
package problems.hard;
import problems.utils.TreeNode;
import java.util.*;
/**
* Created by sherxon on 1/3/17.
*/
public class PostOrderTraversalTree {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list= new ArrayList<>();
if(root==null)return list;
postOrderIt(root, list);
return list;
}
void postOrderRe(TreeNode x, List<Integer> list){
if(x==null)return;
if(x.left!=null)
postOrderRe(x.left, list);
if(x.right!=null)
postOrderRe(x.right, list);
list.add(x.val);
}
void postOrderIt(TreeNode root, List<Integer> list){
Set<TreeNode> set= new HashSet<>();
Stack<TreeNode> stack=new Stack<>();
stack.add(root);
set.add(null);
while(!stack.isEmpty()){
TreeNode peek=stack.peek();
if( (set.contains(peek.left) && set.contains(peek.right))){
list.add(stack.pop().val);
set.add(peek);
}else{
if(peek.right!=null)
stack.add(peek.right);
if(peek.left!=null)
stack.add(peek.left);
}
}
}
}
/*
* Best Solution:
Create an empty stack, Push root node to the stack.
Do following while stack is not empty.
2.1. pop an item from the stack and print it.
2.2. push the left child of popped item to stack.
2.3. push the right child of popped item to stack.
reverse the output.
* */