Discover how to walk through a tree without carrying any extra memory and never get lost!
Why Morris Traversal Inorder Without Stack in DSA Typescript?
Imagine you want to walk through a big garden and look at every flower in order. You try to remember every path you took so you don't get lost or miss any flower. But you have no notebook or map, so you keep getting confused and lost.
Trying to remember all the paths manually is hard and tiring. You might forget where you came from or miss some flowers. Using a notebook (stack) helps, but carrying it around is bulky and slows you down.
Morris Traversal is like cleverly tying small ribbons on branches as you walk, so you can find your way back without carrying a notebook. It lets you visit every flower in order without extra memory, making your walk smooth and easy.
function inorderTraversal(root) {
const stack = [];
let current = root;
while (current || stack.length) {
while (current) {
stack.push(current);
current = current.left;
}
current = stack.pop();
console.log(current.val);
current = current.right;
}
}function morrisInorderTraversal(root) {
let current = root;
while (current) {
if (!current.left) {
console.log(current.val);
current = current.right;
} else {
let predecessor = current.left;
while (predecessor.right && predecessor.right !== current) {
predecessor = predecessor.right;
}
if (!predecessor.right) {
predecessor.right = current;
current = current.left;
} else {
predecessor.right = null;
console.log(current.val);
current = current.right;
}
}
}
}This method enables you to traverse a tree in order using only the tree itself, without extra memory, making your program faster and lighter.
When a robot explores a maze and must remember paths without carrying extra tools, it can use this trick to mark and revisit places efficiently.
Morris Traversal visits nodes in order without extra memory.
It temporarily changes tree links to remember paths.
It is faster and uses less space than stack-based methods.
Practice
Solution
Step 1: Understand space usage in tree traversals
Traditional inorder traversal uses recursion or a stack, which requires extra memory proportional to tree height.Step 2: Morris Traversal space optimization
Morris Traversal cleverly uses threaded binary trees to avoid stack or recursion, thus using O(1) extra space.Final Answer:
It uses no extra space for stack or recursion. -> Option BQuick Check:
Morris Traversal = O(1) space [OK]
- Thinking Morris is faster in time complexity
- Believing Morris only works on BSTs
- Assuming Morris modifies tree permanently
Solution
Step 1: Identify correct initialization type
We start traversal from the root node, so current should be root and allow null for empty trees.Step 2: Check TypeScript syntax
TypeScript requires correct type with possible null, so 'TreeNode | null' is correct.Final Answer:
let current: TreeNode | null = root; -> Option AQuick Check:
Correct type and initialization = let current: TreeNode | null = root; [OK]
- Assigning null without type union
- Starting from root.left instead of root
- Using uninitialized or wrong type
2 / \ 1 3What will be the printed output sequence?
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits left subtree, then node, then right subtree. For this tree, order is 1, 2, 3.Step 2: Morris traversal mimics inorder without stack
Morris traversal outputs nodes in inorder sequence, so output will be [1, 2, 3].Final Answer:
[1, 2, 3] -> Option CQuick Check:
Inorder of given tree = [1, 2, 3] [OK]
- Confusing preorder or postorder with inorder
- Assuming Morris changes output order
- Mixing left and right subtree order
while (current !== null) {
if (current.left === null) {
console.log(current.val);
current = current.right;
} else {
let predecessor = current.left;
while (predecessor.right !== null && predecessor.right !== current) {
predecessor = predecessor.right;
}
if (predecessor.right === null) {
predecessor.right = current;
current = current.left;
} else {
predecessor.right = null;
// Missing console.log here
current = current.right;
}
}
}Solution
Step 1: Identify missing output in else block
When restoring the tree link, the node's value must be printed before moving right, else node is skipped.Step 2: Effect of missing print
Without printing current.val in the unthreading step, nodes with left subtrees are skipped in the output, causing incomplete traversal results.Final Answer:
Missing printing current.val before moving right after restoring link. -> Option DQuick Check:
Missing console.log(current.val) in unthreading step causes skipped nodes [OK]
- Ignoring print statement placement
- Confusing predecessor initialization
- Thinking '===' causes infinite loop
4
/ \
2 5
/ \
1 3
Using Morris inorder traversal, what is the exact sequence of node values printed, and why does this method not use extra memory?Solution
Step 1: Determine inorder traversal sequence
Inorder visits left subtree, node, right subtree. For this tree, sequence is 1, 2, 3, 4, 5.Step 2: Explain Morris traversal memory usage
Morris traversal creates temporary threaded links to predecessor nodes, avoiding stack or recursion, thus using O(1) extra space.Final Answer:
[1, 2, 3, 4, 5]; uses threaded links to avoid stack or recursion. -> Option AQuick Check:
Inorder = [1,2,3,4,5], O(1) space by threading [OK]
- Mixing traversal order
- Assuming recursion or stack used
- Believing tree is permanently changed
Ready to prove it?
Pass the test and this pattern moves unknown
