![]() |
VOOZH | about |
Postorder traversal is a depth-first tree traversal method where each node is processed after its left and right children. While recursive postorder traversal is common, an iterative approach using a single stack offers a more efficient alternative. This article explains how to implement iterative postorder traversal, eliminating recursion while maintaining the correct node visit order, and improving both space and time efficiency.
Refer Iterative Postorder Traversal | Set 1 (Using Two Stacks) for recursive approach
Examples:
Input:
1
/ \
2 3
/ \
4 5
Output: 4 5 2 3 1
Explanation: Postorder traversal (Left->Right->Root) of the tree is 4 5 2 3 1.Input:
8
/ \
1 5
\ / \
7 10 6
\ /
10 6
Output: 10 7 1 6 10 6 5 8
Explanation: Postorder traversal (Left->Right->Root) of the tree is 10 7 1 6 10 6 5 8.
In this iterative version, we push each node onto the stack twice: the first push marks the node, and the second one indicates we need to process it after its children. We keep moving to the left child until we reach the leaf nodes, then start processing nodes by popping them from the stack. While popping if we find stack top() is same as root then go for root->right else print root.
4 5 2 3 1
The idea is to move down to leftmost node using left pointer. While moving down, push root and root's right child to stack. Once we reach leftmost node, print it if it doesn't have a right child. If it has a right child, then change root so that the right child is processed before.
Following is detailed algorithm.
1.1 Create an empty stack
2.1 Do following while root is not NULL
a) Push root's right child and then root to stack.
b) Set root as root's left child.
2.2 Pop an item from stack and set it as root.
a) If the popped item has a right child and the right child
is at top of stack, then remove the right child from stack,
push the root back and set root as root's right child.
b) Else print root's data and set root as NULL.
2.3 Repeat steps 2.1 and 2.2 while stack is not empty.
2 4 5 3 1
Let us consider the following tree
Following are the steps to print postorder traversal of the above tree using one stack.
1. Right child of 1 exists. Push 3 to stack. Push 1 to stack. Move to left child.
Stack: 3, 1
2. Right child of 2 exists. Push 5 to stack. Push 2 to stack. Move to left child.
Stack: 3, 1, 5, 2
3. Right child of 4 doesn't exist. Push 4 to stack. Move to left child.
Stack: 3, 1, 5, 2, 4
4. Current node is NULL. Pop 4 from stack. Right child of 4 doesn't exist. Print 4. Set current node to NULL.
Stack: 3, 1, 5, 2
5. Current node is NULL. Pop 2 from stack. Since right child of 2 equals stack top element, pop 5 from stack. Now push 2 to stack. Move current node to right child of 2 i.e. 5
Stack: 3, 1, 2
6. Right child of 5 doesn't exist. Push 5 to stack. Move to left child.
Stack: 3, 1, 2, 5
7. Current node is NULL. Pop 5 from stack. Right child of 5 doesn't exist. Print 5. Set current node to NULL.
Stack: 3, 1, 2
8. Current node is NULL. Pop 2 from stack. Right child of 2 is not equal to stack top element. Print 2. Set current node to NULL.
Stack: 3, 1
9. Current node is NULL. Pop 1 from stack. Since right child of 1 equals stack top element, pop 3 from stack. Now push 1 to stack. Move current node to right child of 1 i.e. 3
Stack: 1
10. Repeat the same as above steps and Print 6, 7 and 3.
Pop 1 and Print 1.
Time Complexity: O(n), where n is the number of nodes, as each node is visited once during traversal.
Auxiliary Space: O(h), where h is the height of the tree, due to the stack, with h being O(n) in the worst case for a skewed tree.
The traversal order in postorder is left child, right child, and then the node itself. Instead of recursion, the algorithm simulates the recursive calls using a stack. It first pushes nodes onto the stack as it moves down the left side of the tree. Once it reaches a leaf, it starts processing the nodes by checking if the right child has been visited. If not, it traverses the right subtree; otherwise, it processes the node and adds it to the result.
Post order traversal of binary tree is: [4 5 2 6 7 3 1 ]