Given a binary tree, write iterative and recursive solution to traverse the tree using post-order traversal.
Unlike linked lists, one-dimensional arrays and other linear data structures, which are traversed in linear order, trees may be traversed in multiple ways in depth-first order (post-order, pre-order and post-order) or breadth-first order (level order traversal). Beyond these basic traversals, various more complex or hybrid schemes are possible, such as depth-limited searches like iterative deepening depth-first search.
In this post, postorder tree traversal is discussed in detail.
Traversing a tree involves iterating over all nodes in some manner. As tree is not a linear data structure, from a given node there can be more than one possible next node, so some nodes must be deferred i.e stored in some way for later visiting. The traversal can be done iteratively where the deferred nodes are stored in the stack or it can be done by recursion where the deferred nodes are stored implicitly in the call stack.
For traversing a (non-empty) binary tree in post-order fashion, we must do these three things for every node N starting from root node of the tree:
(R) Recursively traverse its right subtree. When this step is finished we are back at N again.
(N) Process N itself
In normal post-order traversal we visit left subtree before the right subtree. If we visit right subtree before visiting the left subtree, it is referred as reverse post-order traversal.
As we can see before processing any node, the left subtree is processed first, followed by the right subtree and the node is processed at last. These operations can be defined recursively for each node. The recursive implementation is referred to as depth-first search (DFS), as the search tree is deepened as much as possible on each child before going to the next sibling.
C++ implementation –
// Recursive function to perform post-order traversal of the tree
void postorder(Node *root)
// if the current node is empty
if (root == nullptr)
// Traverse the left subtree
// Traverse the right subtree
// Display the data part of the root (or current node)
cout << root->data << " ";
Iterative implementation –
To convert above recursive procedure to iterative one, we need an explicit stack. Below is a simple stack based iterative algorithm to do post-order traversal.
s -> empty stack
t -> output stack
while (not s.isEmpty())
node -> s.pop()
if (node.left <> null)
if (node.right <> null)
while (not t.isEmpty())
node -> t.pop()
C++ implementation –
// Iterative function to perform post-order traversal of the tree
void postorderIterative(Node* root)
// create an empty stack and push root node
// create another stack to store post-order traversal
// run till stack is not empty
// we pop a node from the stack and push the data to output stack
Node *curr = stk.top();
// push left and right child of popped node to the stack
// print post-order traversal
cout << out.top() << " ";
The time complexity of above solutions is O(n) and space complexity of the program is O(n) as space required is proportional to the height of the tree which can be equal to number of nodes in the tree in worst case for skewed trees.
Exercise: Do iterative post-order traversal using only one stack.
Thanks for reading.