Morris Inorder Traversal
Inorder traversal in O(1) space — no recursion, no stack.
Open on GeeksforGeeks ↗Intuition
The stack's only job is remembering how to get back up. Morris threading stores that return path in the tree itself: link the left subtree's rightmost node (the inorder predecessor) back to the current node. Coming back later, the thread's existence tells you the left side is done — remove it and visit.
Approach
Find the predecessor
cur has a left child → walk left subtree's right spine to its end (stopping if the thread back to cur already exists).
Thread or visit
No thread yet → create it (pred.right = cur), descend left. Thread exists → left subtree finished: cut the thread, visit cur, go right.
Tree restored
Every thread is removed on second arrival — the tree ends unchanged, and each edge is walked ≤ 3 times → O(n).
Solution & live demo
Edge cases
Visit immediately and go right — the trivial case.
Threads would be left dangling — Morris must run to completion to restore the tree.