Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Iterative Post Order Traversal Algorithm on the SSaurel's Channel
public static <T> void iterativePostOrderTraverse(Node<T> node) {
if (node == null)
// We create two stacks
Stack<Node<T>> nodeStack1 = new Stack<>();
Stack<Node<T>> nodeStack2 = new Stack<>();
// We push root to first stack
// We iterate while first stack is not empty
while (!nodeStack1.isEmpty()) {
// We pop an item from nodeStack1 and we push it to nodeStack2
Node<T> tmpNode = nodeStack1.pop();
// We push left and right children of removed item to nodeStack1
if (tmpNode.left != null)
if (tmpNode.right != null)
// We print all elements of nodeStack2
while (!nodeStack2.isEmpty()) {
Node<T> tmpNode = nodeStack2.pop();
System.out.print( + " ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment