VOOZH about

URL: https://www.geeksforgeeks.org/dsa/iterative-preorder-traversal/

⇱ Iterative Preorder Traversal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Iterative Preorder Traversal

Last Updated : 23 Jul, 2025

Given a binary tree, write an iterative function to print the Preorder traversal of the tree. The Preorder traversal follows the order: Root → Left → Right.

Examples:

Input:
1
/ \
2 3
/ \
4 5
Output: 1 2 4 5 3
Explanation: Preorder traversal (Root->Left->Right) of the tree is 1 2 4 5 3.

Input:
8
/ \
1 5
\ / \
7 10 6
\ /
  10 6
Output: 8 1 7 10 5 10 6 6 
Explanation:
Preorder traversal (Root->Left->Right) of the tree is 8 1 7 10 5 10 6 6.


[Naive Approach] Simple Iterative Preorder (with stack) - O(n) time and O(n) space

Following is a simple stack based iterative process to print Preorder traversal. 

  1. Create an empty stack and push root node to stack. 
  2. Do the following while is not empty. 
    1. Pop an item from the stack and print it. 
    2. Push right child of a popped item to stack 
    3. Push left child of a popped item to stack

The right child is pushed before the left child to make sure that the left subtree is processed first.


Output
1 2 4 5 3 

[Better Approach] Iterative Preorder with Current Pointer (with stack) - O(n) time and O(n) space

In the previous solution we can see that the left child is popped as soon as it is pushed to the stack, therefore it is not required to push it into the stack. 

The idea is to start traversing the tree from the root node and keep printing the left child while exists and simultaneously, push the right child of every node in an auxiliary stack. Once we reach a null node, pop a right child from the auxiliary stack and repeat the process while the auxiliary stack is not empty. 


Output
1 2 4 5 3 

[Expected Approach] Preorder Morris Traversal - O(n) time and O(1) space

Morris Traversal is a tree traversal technique that allows you to traverse a binary tree without using recursion or a stack. The Preorder Morris Traversal algorithm works by manipulating the tree's pointers, specifically by utilizing the inorder predecessor of each node.

Algorithm for Preorder Morris Traversal:

1. If the left child of the current node is NULL:

  • Print the current node's data (since it is the root of the subtree).
  • Move to the right child of the current node.

2. If the left child of the current node is NOT NULL:

  • Find the inorder predecessor of the current node: The inorder predecessor is the rightmost node of the left subtree of the current node.

Two cases arise:

a) The right child of the inorder predecessor already points to the current node:

  • This means we've already visited this node, so we can set the right child of the inorder predecessor to NULL.
  • Move to the right child of the current node.

b) The right child of the inorder predecessor is NULL:

  • Set the right child of the inorder predecessor to point to the current node.
  • Print the current node's data.
  • Move to the left child of the current node (this is the new node to visit next).

3. Repeat the process until the current node is NULL.


Output
1 2 4 5 3 

Refer Preorder Traversal of Binary Tree for recursive code

Comment
Article Tags:
Article Tags: