[Solved] PostOrder Traversal of Binary Tree Without Recursion (Iterative)

[Solved] PostOrder Traversal of Binary Tree Without Recursion (Iterative)

Traversing elements in postorder traversal with recursion is easy. You can read the postorder traversal in detail where I have explained it with binary tree example and code.

Problem Statement

You have given a binary tree. Implement PostOrder Traversal Without Recursion.

Here we are going to use the iterative method to traverse binary tree elements

(This problem was asked in the OVH cloud interview round. You have to write working code on the whiteboard.)

Binary Tree Example

reorder, inorder and post order traversal

The postorder traversal of the Binary tree is 7 3 1 6 5 4.

Algorithm

Here we are going to use the stack. Each element in the stack will maintain the current node and the flag. The flag will be set to True if the children of the current node are not visited.

In Python, the stack can be implemented using a Python list.

  • Create a binary tree if it is not given
  • Add the root node in the stack (list)
  • While there is an element in the stack (list)
    • Read an element from the stack (node, flag).
    • If the flag is true, set the flag as false. Add right and left children to the stack.
    • Else, print the value of the current node. Pop the node from the stack (Remove the last element from the list).

If you understand the logic and algorithm, you can implement it in many programming languages like C/C++, Java, Python…

Python Program for PostOrder Traversal without Recursion (Iterative)

	
#structure/class for a node
#by defult, left and right pointers are None 
class node:
  def __init__(self, val, left=None, right=None):
    self.left=left
    self.val=val
    self.right=right
  
  
#adding element in the binary tree
root=node(4)
root.left=node(1)
root.right=node(5)
root.left.left=node(7)
root.left.right=node(3)
root.right.right=node(6)
 
#return True if the node has child
#retun False if node has no child
def isChild(node):
    if node.right or node.left:
        return True
    else:
        return False

#print post order traversal
#without recursion
def printPostOrderWithoutRec(node):
    liNode = []
    if node:
        liNode.append([node, isChild(node)])
    else:
        print("Tree is empty.")
        return

    while liNode:
        curNode, bChild = liNode[-1]

        if bChild:
            liNode[-1][1] = 0
            if curNode.right:
                liNode.append([curNode.right, isChild(curNode.right)])
            if curNode.left:
                liNode.append([curNode.left, isChild(curNode.left)])
        else:
            print(curNode.val)
            del liNode[-1]

printPostOrderWithoutRec(root)

Output:

7
3
1
6
5
4

What’s Next?

I hope you understood postorder traversal without recursion. You can practice implementing inorder and preorder traversal without recursion.

If you are preparing for an interview with product-based companies, you have to prepare for a binary tree. If you have any queries, you can comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *