VOOZH about

URL: https://www.geeksforgeeks.org/dsa/inorder-tree-traversal-without-recursion/

⇱ Inorder Tree Traversal without Recursion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Inorder Tree Traversal without Recursion

Last Updated : 5 Apr, 2026

Given a binary tree, the task is to perform in-order traversal of the tree without using recursion.

Example:

Input:

👁 Iterative-Postorder-Traversal

Output: 4 2 5 1 3
Explanation: Inorder traversal (Left->Root->Right) of the tree is 4 2 5 1 3

Input:

👁 Iterative-Postorder-Traversal-2

Output: 1 7 10 8 6 10 5 6
Explanation: Inorder traversal (Left->Root->Right) of the tree is 1 7 10 8 6 10 5 6

Recap of the Inorder Traversal


[Naive Approach] Using Stack - O(n) Time and O(h) Space

The idea is to implement recursion using a stack. Starting from root node, keep on pushing the node into a stack and move to left node. When node becomes null, pop a node from the stack, print its value and move to the right node.



Output
4 2 5 1 3 

Let us do a dry run for the below tree for example:

👁 Iterative-Postorder-Traversal


Initially Creates an empty stack: S = NULL and set current as address of root: current -> 1

  1. Starting at Root (Node 1): current = 1
    • Push 1 to the stack. Stack S = [1]
    • Move current to theleft child: current = 2
  2. At Node 2:
    • Push 2 to the stack. Stack S = [2, 1]
    • Move current to the left child: current = 4
  3. At Node 4:
    • Push 4 to the stack. Stack S = [4, 2, 1]
    • Move current to the left child: current = NULL (Node 4 has no left child)
  4. current is NULL:
    • Pop 4 from the stack. Stack S = [2, 1]
    • Print 4
    • Move current to the right child of Node 4: current = NULL (Node 4 has no right child)
  5. Repeat with current = NULL:
    • Pop 2 from the stack. Stack S = [1]
    • Print 2
    • Move current to the right child of Node 2: current = 5
  6. At Node 5:
    • Push 5 to the stack. Stack S = [5, 1]
    • Move current to the left child: current = NULL (Node 5 has no left child)
  7. current is NULL:
    • Pop 5 from the stack. Stack S = [1]
    • Print 5
    • Move current to the right child of Node 5: current = NULL (Node 5 has no right child)
  8. Repeat with current = NULL:
    • Pop 1 from the stack. Stack S = []
    • Print 1
    • Move current to the right child of Node 1: current = 3
  9. At Node 3:
    • Push 3 to the stack. Stack S = [3]
    • Move current to the left child: current = NULL (Node 3 has no left child)
  10. current is NULL:
    • Pop 3 from the stack. Stack S = []
    • Print 3
    • Move current to the right child of Node 3: current = NULL (Node 3 has no right child)

[Expected Approach] Using Morris Traversal Algorithm - O(n) Time and O(1) Space

Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree. Please Refer to Inorder Tree Traversal without recursion and without stack! for implementation.

Comment
Article Tags: