Morris Preorder Traversal
Morris Preorder Traversal is a GFG problem (Hard). Given the root of a binary tree, return its preorder traversal: the node first, then its left subtree, then its right subtree.
- The walk may use only O(1) extra space, so no recursion and no stack. The returned list is not counted.
- Pointers may be changed during the walk, as long as every one is restored before you return.
- 1 <= number of nodes <= 3 * 10⁴
- 0 <= node->data <= 10⁵
- O(1) extra space: no recursion and no stack
Intuition
A stack-based preorder remembers where each right subtree starts. The Morris walk keeps that memory in the tree instead.
Before entering the left subtree of cur, find the node that finishes it: the rightmost node of cur.left. Nothing hangs off its right side, so aim that empty pointer at cur. When the walk reaches that node later, the borrowed link leads straight back to cur, and from there the walk turns right.
Preorder writes cur at the moment the link is laid, because a node must come before everything in its left subtree. Meeting the link a second time only removes it.
When a preorder walk may use neither recursion nor a stack, only O(1) extra space, the answer is the Morris walk. It also fits problems that flatten or serialise a tree in preorder while being told not to use extra memory.
Approach
Before reading on, work out which node of cur's left subtree is written last in preorder, and why its right pointer is free to borrow. Then decide at which of the two arrivals at cur the value should be written.
Write a node that has no left child
If cur.left is empty there is no left subtree to come back from. Write cur and move to cur.right, which is either a real child or a link that returns the walk to an ancestor.
Find the node that ends the left subtree
From cur.left, move right while the right pointer is set and is not cur. You stop either at the rightmost node, whose right pointer is empty, or at the node already linked back to cur.
First arrival: write, link, go left
If that right pointer is empty, this is the first time at cur. Write cur now, set the pointer to cur, and move to cur.left. Writing here puts cur ahead of its whole left subtree, which is exactly preorder.
Second arrival: unlink and go right
If the pointer already holds cur, the walk has finished the left subtree and climbed back. Set the pointer to empty and move to cur.right, writing nothing: cur went into the answer on its first arrival.
Why every node is written exactly once
Links only ever point at nodes that have a left child. So a node without one is reached once and written in the first branch, and a node with one is reached twice and written only on the first of those arrivals.
Code in Python, C++ and Java
Common pitfalls
Writing on the second arrival
else:
pred.right = None
res.append(cur.data)
cur = cur.rightelse:
pred.right = None
cur = cur.rightBy the second arrival the left subtree is already in the answer, so writing cur here puts it after its left children. With the write moved out of the first branch, [1, 2, 3] gives [2, 1, 3], which is inorder.
Writing in both branches
if pred.right is None:
res.append(cur.data)
...
else:
res.append(cur.data)
...write cur only when the link is laid
This happens when an inorder version is edited by adding the preorder write without removing the old one. Every node with a left child then appears twice: [1, 2] gives [1, 2, 1].
Leaving out the write for a node with no left child
if cur.left is None:
cur = cur.rightif cur.left is None:
res.append(cur.data)
cur = cur.rightSuch a node is never linked to, so this branch is its only chance to be written. Every leaf goes missing: [1, 2, 3] gives [1].
Edge cases
No left subtree exists anywhere, so no link is laid and each node is written by the first branch as the walk moves right.
The walk from 2 goes right to 3, so the link runs from 3 back to 1, not from 2. Linking from cur.left itself would overwrite 2's real right pointer and lose node 3.
Complexity
cur and pred are stored.Morris Preorder Traversal FAQ
What is the difference between Morris preorder and Morris inorder?
Only where the write happens. Inorder writes cur when the link is removed, after its left subtree; preorder writes it when the link is laid.
Is there a Morris postorder traversal?
Yes, still in O(1) space, but it is more work. Use a dummy node whose left child is the root. On each second arrival at cur, write the right edge path from cur.left down to the predecessor in reverse, by reversing those right pointers, reading them, and reversing them back.
Why is the borrowed right pointer always empty?
The node is the rightmost one in cur.left's subtree, found by moving right until there is nowhere to go. If it had a right child, the walk would have moved on to that child.