Morris Traversal
postorder traversal
binary tree
algorithms
data structures

Can we use Morris Traversal for postorder?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, Morris traversal can be adapted for postorder traversal, but it is more complex than inorder or preorder variants. The method avoids recursion and explicit stack by using temporary threaded pointers. Correct implementation requires careful boundary reversal and full restoration of modified links.

Why Postorder Is the Hard Case

Postorder requires left subtree, then right subtree, then node. Without recursion stack, you need another way to emit nodes after both subtrees are processed.

Morris traversal solves this by:

  • creating temporary right-thread links
  • detecting when traversal returns from a left subtree
  • outputting nodes along a boundary in reverse order
  • restoring links immediately afterward

The reverse-output step is the key complexity.

Core Building Blocks

A robust Morris postorder implementation typically uses helper functions:

  • reverse right pointers along a path
  • visit nodes on reversed path
  • restore original path afterward
python
1class Node:
2    def __init__(self, val, left=None, right=None):
3        self.val = val
4        self.left = left
5        self.right = right
6
7
8def reverse_path(start, end):
9    prev = None
10    cur = start
11    while prev is not end:
12        nxt = cur.right
13        cur.right = prev
14        prev = cur
15        cur = nxt
16
17
18def collect_reverse(start, end, output):
19    reverse_path(start, end)
20    node = end
21    while True:
22        output.append(node.val)
23        if node is start:
24            break
25        node = node.right
26    reverse_path(end, start)

Restoration is mandatory. If path is not restored, tree structure is damaged.

Full Morris Postorder Algorithm

A common pattern uses a dummy node whose left child is the real root. This makes boundary handling uniform.

python
1def morris_postorder(root):
2    dummy = Node(0, left=root)
3    output = []
4    cur = dummy
5
6    while cur:
7        if cur.left is None:
8            cur = cur.right
9        else:
10            pred = cur.left
11            while pred.right and pred.right is not cur:
12                pred = pred.right
13
14            if pred.right is None:
15                pred.right = cur
16                cur = cur.left
17            else:
18                collect_reverse(cur.left, pred, output)
19                pred.right = None
20                cur = cur.right
21
22    return output
23
24# demo tree
25root = Node(1,
26            Node(2, Node(4), Node(5)),
27            Node(3, None, Node(6)))
28
29print(morris_postorder(root))  # expected [4, 5, 2, 6, 3, 1]

This runs in linear time and uses constant auxiliary space beyond output list.

When This Approach Is Worth Using

Morris postorder is useful when stack usage must be minimized, such as deeply skewed trees in constrained environments. It is also valuable for interview and algorithm training where pointer manipulation is the focus.

In many production systems, iterative stack-based postorder is simpler and easier to maintain. If constant extra space is not a hard requirement, readability may outweigh Morris-style space optimization.

Verification and Safety Checks

Because pointer threading mutates tree links temporarily, validation is important:

  • traverse same tree twice and ensure outputs match expected
  • verify tree structure is unchanged after traversal
  • test edge cases including empty tree and single-node tree
  • test skewed trees where recursion would be deepest

If second traversal output differs unexpectedly, restoration logic is likely incorrect.

Complexity and Tradeoffs

Morris postorder complexity:

  • time complexity is linear in node count
  • auxiliary space is constant, excluding output list

Tradeoff:

  • lower memory overhead
  • higher implementation complexity and bug risk

Use this technique intentionally where its benefits matter.

Common Pitfalls

Forgetting to restore threaded links corrupts tree structure after traversal.

Reversing boundary path in only one direction causes broken pointers or wrong output order.

Emitting nodes at wrong phase produces preorder-like or inorder-like sequences.

Using Morris method by default when readability and maintainability are the higher priority.

Summary

  • Morris traversal can be adapted for postorder traversal.
  • The method relies on temporary threading plus boundary reversal.
  • Dummy-root technique simplifies edge handling.
  • Link restoration is required for correctness.
  • Choose Morris postorder when constant extra space is a real requirement.

Course illustration
Course illustration

All Rights Reserved.