Morris Traversal (Threaded Binary Tree)
Morris Traversal is a brilliant tree traversal technique that achieves O(1) space complexity without using recursion or an explicit stack. Invented by Joseph H. Morris in 1979, it temporarily modifies the tree structure by creating 'threads' (temporary links) that allow the algorithm to traverse back to ancestors. After traversal, the original tree structure is restored. This makes it ideal for memory-constrained environments and demonstrates creative problem-solving in algorithm design.
Visualization
Interactive visualization for Morris Traversal (Threaded Binary Tree)
Morris Traversal Visualization
Inorder tree traversal with O(1) space using threaded binary tree technique
Time Complexity: O(n)
Space Complexity: O(1) - No stack/recursion!
How it works:
- Creates temporary "threads" to parent nodes
- Uses rightmost node of left subtree as predecessor
- Thread points back to current node temporarily
- Removes thread when returning to parent
- Achieves O(1) space without recursion or stack
- Named after J. H. Morris (1979)
Interactive visualization with step-by-step execution
Implementation
1function morrisInorder(root: TreeNode | null): number[] {
2 const result: number[] = [];
3 let current = root;
4
5 while (current) {
6 if (!current.left) {
7 result.push(current.val);
8 current = current.right;
9 } else {
10 // Find predecessor
11 let predecessor = current.left;
12 while (predecessor.right && predecessor.right !== current) {
13 predecessor = predecessor.right;
14 }
15
16 if (!predecessor.right) {
17 // Create thread
18 predecessor.right = current;
19 current = current.left;
20 } else {
21 // Remove thread
22 predecessor.right = null;
23 result.push(current.val);
24 current = current.right;
25 }
26 }
27 }
28
29 return result;
30}Deep Dive
Theoretical Foundation
Morris Traversal exploits the fact that in inorder traversal, a node is visited after its left subtree. The algorithm creates temporary links from the rightmost node of each left subtree to its inorder successor. These 'threads' enable returning to the parent without a stack. For each node with a left child, find the inorder predecessor (rightmost node in left subtree). If its right pointer is null, create thread to current node and go left. If thread already exists, we're returning from left subtree: visit current, remove thread, go right. Time: O(n) - each node visited at most 3 times. Space: O(1) - no recursion or stack, only constant variables. Tree structure temporarily modified but fully restored.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(1)
Applications
Industry Use
Embedded systems with limited memory
Tree traversal in memory-constrained devices
In-place tree processing
Memory-efficient tree serialization
Use Cases
Related Algorithms
AVL Tree (Self-Balancing BST)
A self-balancing binary search tree where the heights of the two child subtrees of any node differ by at most one. Named after inventors Adelson-Velsky and Landis (1962), AVL trees guarantee O(log n) time for search, insert, and delete operations by maintaining balance through rotations. It was the first self-balancing binary search tree to be invented.
Segment Tree (Range Query Tree)
Segment Tree is a binary tree data structure for storing intervals/segments that enables efficient range queries (sum, min, max, GCD) and updates in O(log n) time. Each node represents an interval, with leaves representing single elements. Essential for competitive programming, it solves problems like range sum queries with updates, which would be O(n) with arrays but becomes O(log n) with segment trees.
Fenwick Tree (Binary Indexed Tree - BIT)
A data structure that can efficiently update elements and calculate prefix sums in O(log n) time. Also known as Binary Indexed Tree (BIT), invented by Peter Fenwick in 1994. Fenwick trees are more space-efficient and simpler than segment trees for range sum queries, using only O(n) space compared to segment tree's O(4n).
Lowest Common Ancestor (LCA)
Lowest Common Ancestor (LCA) finds the deepest node that is an ancestor of two given nodes in a tree. This fundamental tree query operation has applications in computational biology (phylogenetic trees), version control systems (finding merge base), network routing, and range queries. Multiple algorithms exist: simple recursive O(n), binary lifting O(log n) per query after O(n log n) preprocessing, and Tarjan's offline algorithm for batch queries.