Created
August 20, 2015 05:47
-
-
Save thmain/ad3c733af9eca5b65e08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package T_PostOrder; | |
import java.util.Stack; | |
public class PostorderTree { | |
public void postOrderRecursive(Node root) { | |
if (root != null) { | |
postOrderRecursive(root.left); | |
postOrderRecursive(root.right); | |
System.out.print(root.data + " "); | |
} | |
} | |
public void preorderIteration(Node root) { | |
Stack<Node> s = new Stack<Node>(); | |
while (true) { | |
// First print the root node and then add left node | |
while (root != null) { | |
System.out.print(root.data + " "); | |
s.push(root); | |
root = root.left; | |
} | |
// check if Stack is emtpy, if yes, exit from everywhere | |
if (s.isEmpty()) { | |
return; | |
} | |
// pop the element from the stack and go right to the tree | |
root = s.pop(); | |
root = root.right; | |
} | |
} | |
public static void main(String[] args) { | |
Node root = new Node(10); | |
root.left = new Node(20); | |
root.right = new Node(30); | |
root.left.left = new Node(5); | |
root.left.right = new Node(15); | |
PostorderTree i = new PostorderTree(); | |
i.postOrderRecursive(root); | |
System.out.println(); | |
//i.preorderIteration(root); | |
} | |
} | |
class Node { | |
int data; | |
Node left; | |
Node right; | |
public Node(int data) { | |
this.data = data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment