We have discussed a simple iterative postorder traversal using two stacks in the previous post. In this post, an approach with only one stack is discussed.

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.

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.

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20iterative%20postorder%20traversal%0A%23%20using%20one%20stack%0A%20%0A%23%20Stores%20the%20answer%0Aans%20%3D%20%5B%5D%0A%20%0A%23%20A%20Binary%20tree%20node%0Aclass%20Node%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.left%20%3D%20None%0A%20%20%20%20%20%20%20%20self.right%20%3D%20None%0A%20%0Adef%20peek(stack)%3A%0A%20%20%20%20if%20len(stack)%20%3E%200%3A%0A%20%20%20%20%20%20%20%20return%20stack%5B-1%5D%0A%20%20%20%20return%20None%0A%23%20A%20iterative%20function%20to%20do%20postorder%20traversal%20of%20%0A%23%20a%20given%20binary%20tree%0Adef%20postOrderIterative(root)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%20Check%20for%20empty%20tree%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%20%20%20%20%20%0A%20%20%20%20while(True)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(root)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Push%20root’s%20right%20child%20and%20then%20root%20to%20stack%0A%20%20%20%20%20%20%20%20%20%20%20%20%20if%20root.right%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20stack.append(root.right)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20stack.append(root)%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Set%20root%20as%20root’s%20left%20child%0A%20%20%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20root.left%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Pop%20an%20item%20from%20stack%20and%20set%20it%20as%20root%0A%20%20%20%20%20%20%20%20root%20%3D%20stack.pop()%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20the%20popped%20item%20has%20a%20right%20child%20and%20the%0A%20%20%20%20%20%20%20%20%23%20right%20child%20is%20not%20processed%20yet%2C%20then%20make%20sure%0A%20%20%20%20%20%20%20%20%23%20right%20child%20is%20processed%20before%20root%0A%20%20%20%20%20%20%20%20if%20(root.right%20is%20not%20None%20and%0A%20%20%20%20%20%20%20%20%20%20%20%20peek(stack)%20%3D%3D%20root.right)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20stack.pop()%20%23%20Remove%20right%20child%20from%20stack%20%0A%20%20%20%20%20%20%20%20%20%20%20%20stack.append(root)%20%23%20Push%20root%20back%20to%20stack%0A%20%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20root.right%20%23%20change%20root%20so%20that%20the%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20righ%20childis%20processed%20next%0A%20%0A%20%20%20%20%20%20%20%20%23%20Else%20print%20root’s%20data%20and%20set%20root%20as%20None%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ans.append(root.data)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20None%0A%20%0A%20%20%20%20%20%20%20%20if%20(len(stack)%20%3C%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%0A%23%20Driver%20pogram%20to%20test%20above%20function%0Aroot%20%3D%20Node(1)%0Aroot.left%20%3D%20Node(2)%0Aroot.right%20%3D%20Node(3)%0Aroot.left.left%20%3D%20Node(4)%0Aroot.left.right%20%3D%20Node(5)%0Aroot.right.left%20%3D%20Node(6)%0Aroot.right.right%20%3D%20Node(7)%0A%20%0Aprint%20%22Post%20Order%20traversal%20of%20binary%20tree%20is%22%0ApostOrderIterative(root)%0Aprint%20ans%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh(nickzuck_007)” message=”” highlight=”” provider=”manual”/]

Output:

Post Order traversal of binary tree is
[4, 5, 2, 6, 7, 3, 1]

Categorized in: