VOOZH about

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

⇱ Morris traversal for Postorder - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Morris traversal for Postorder

Last Updated : 8 Oct, 2025

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

Examples:

Input:

👁 Iterative-Postorder-Traversal

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

Input:  

👁 Iterative-Postorder-Traversal-2

Output: [10, 7, 1, 6, 10, 6, 5, 8]
Explanation: Postorder traversal (Left->Right->Root) of the tree is 10, 7, 1, 6, 10, 6, 5, 8.

Approach:

In Postorder traversal, we visit nodes in Left - Right - Node (LRN) order, which means each node is visited only after its left and right subtrees. Implementing this directly with Morris Traversal is tricky because we need to know when both subtrees are fully visited. To simplify, we use a mirrored Preorder approach: traverse the tree in Node - Right - Left (NRL) order, visiting each node when we first encounter it, then moving to the right subtree, and finally to the left subtree. During this traversal, we store the nodes in an array. Finally, by reversing the array, the NRL order becomes LRN, giving the correct Postorder sequence.

Morris Postorder Traversal Steps

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

  • If the node does not have a right child, visit(store) the node and move to the left child.
  • If the node has a right child, find the leftmost node in the right subtree.
  • If the leftmost node’s left is NULL. Make the current node as the left child of this leftmost node (temporary link).Visit (store) the current node and move to the right child.
  • If the leftmost node’s left points to the current node. Remove the temporary link (leftmost->left = NULL) and move to the left child.
  • Repeat until curr == NULL.

After traversal, reverse the stored array. This converts Node-Right-Left (NRL) order into Left-Right-Node (LRN) postorder.



Output
6 7 2 8 9 3 1 

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

Comment