VOOZH about

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

⇱ Morris traversal for Preorder - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Morris traversal for Preorder

Last Updated : 9 Oct, 2025

Given the root of a binary tree, Find its Preorder traversal using Morris Traversal, i.e., without using recursion or a stack.

Examples

Input:

👁 Iterative-Postorder-Traversal

Output: [1, 2, 4, 5, 3]
Explanation: Preorder traversal (Root->Left->Right) of the tree is 1, 2, 4, 5, 3.

Input:

👁 Iterative-Postorder-Traversal-2

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.

Approach:

The key idea is to temporarily establish links to a node’s inorder predecessor so that we can traverse the tree and print the data in preorder. After visiting the nodes, we revert these links to restore the tree to its original structure. Although the tree is temporarily modified during traversal, it remains unchanged once the traversal is complete.

Morris Preorder Traversal Steps

Start with root Node(curr) and for each node:

  • If the node does not have a left child, Visit (store) the node and move to the right child.
  • If the node has a left child, Find the inorder predecessor (rightmost node in the left subtree).
  • If the predecessor’s right is NULL, Make the current node as the right child of its inorder predecessor (temporary link). Visit(store) the current node and Move to the left child.
  • If the predecessor’s right points to the current node.Remove the temporary link (restore predecessor->right = NULL) and move to the right child.
  • Repeat until curr becomes NULL.

Output
1 2 4 5 3 

Time Complexity: O(n)
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: