Repository navigation
Expand file tree
/
Copy pathPostOrderIterative.java
More file actions
46 lines (43 loc) · 1.64 KB
/
Copy pathPostOrderIterative.java
File metadata and controls
46 lines (43 loc) · 1.64 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
/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
* E-Mail : info@careermonk.com
* Creation Date : 2015-01-10 06:15:46
* Last modification : 2006-05-31
by : Narasimha Karumanchi
* Book Title : Data Structures And Algorithms Made In Java
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
*
*/
package chapter06trees;
import java.util.ArrayList;
import java.util.Stack;
public class PostOrderIterative {
public ArrayList<Integer> postorderTraversal(BinaryTreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
Stack<BinaryTreeNode> s = new Stack<BinaryTreeNode>();
s.push(root);
BinaryTreeNode prev = null;
while(!s.isEmpty()){
BinaryTreeNode curr = s.peek();
if(prev == null || prev.getLeft() == curr || prev.right == curr){
// traverse from top to bottom, and if curr has left child or right child,
// push into the stack; otherwise, pop out.
if(curr.getLeft() != null)
s.push(curr.getLeft());
else if(curr.right != null)
s.push(curr.right);
}else if(curr.getLeft() == prev){
if(curr.right != null)
s.push(curr.right);
}else{
res.add(curr.data);
s.pop();
}
prev = curr;
}
return res;
}
}