What if you could walk through a tree without carrying any extra baggage or losing your way?
Why Morris Traversal Inorder Without Stack in DSA Javascript?
Imagine you have a big family tree drawn on paper, and you want to visit every family member in order. You try to remember where you left off by writing notes or using bookmarks, but it gets messy and confusing.
Using extra notes or bookmarks (like a stack) to remember where you are slows you down and can make you lose track. It also uses extra space, which is like carrying a heavy backpack while walking through the tree.
Morris Traversal lets you visit every family member in order without carrying any extra notes or bookmarks. It cleverly changes the tree paths temporarily to remember where to go next, then fixes the tree back to normal.
function inorderTraversal(root) {
let 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;
}
}
}
}You can traverse a binary tree in order using only the tree itself, saving memory and making your code faster and cleaner.
When working with huge trees in limited memory devices, like embedded systems or mobile apps, Morris Traversal helps you process data without extra memory overhead.
Morris Traversal visits nodes without extra memory like stacks or recursion.
It temporarily changes tree links to remember paths, then restores them.
This method is efficient for memory-limited environments.
Practice
Solution
Step 1: Understand Morris Traversal space usage
Morris Traversal modifies tree links temporarily to avoid using stack or recursion, so it uses O(1) extra space.Step 2: Compare with traditional methods
Traditional inorder traversal uses stack or recursion which requires O(h) space, where h is tree height.Final Answer:
It uses no extra space like stack or recursion -> Option DQuick Check:
Morris Traversal = O(1) space [OK]
- Thinking Morris Traversal is faster in time complexity
- Assuming it only works on binary search trees
- Believing it uses extra data structures like queues
curr in Morris Traversal?Solution
Step 1: Identify inorder predecessor in Morris Traversal
The inorder predecessor is the rightmost node in the left subtree ofcurr.Step 2: Traverse left subtree then rightmost path
Start atcurr.left, move right repeatedly until right child is null or points back tocurr.Final Answer:
Move to the left child ofcurrand then keep moving right until right child is null orcurr-> Option AQuick Check:
Inorder predecessor = left subtree's rightmost node [OK]
- Moving right child first instead of left
- Stopping after one left move without going right
- Confusing predecessor with successor
2 / \ 1 3Assuming the traversal prints node values in order.
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits left subtree, then current node, then right subtree.Step 2: Apply Morris traversal on given tree
Left child is 1, root is 2, right child is 3, so output is [1, 2, 3].Final Answer:
[1, 2, 3] -> Option CQuick Check:
Inorder = left, root, right = [1, 2, 3] [OK]
- Confusing preorder or postorder with inorder
- Mixing node visit order
- Assuming traversal prints in reverse
while (curr !== null) {
if (curr.left === null) {
console.log(curr.val);
curr = curr.right;
} else {
let pred = curr.left;
while (pred.right !== null && pred.right !== curr) {
pred = pred.right;
}
if (pred.right === null) {
pred.right = curr;
curr = curr.left;
} else {
pred.right = null;
console.log(curr.val);
curr = curr.right;
}
}
}
What is the error in this code?Solution
Step 1: Check when node values are printed
Values are printed only whencurr.left === null, but after restoring links, printing is missing.Step 2: Identify missing print after restoring links
After settingpred.right = null, the code movescurr = curr.rightwithout printingcurr.val.Final Answer:
It misses printing the current node after restoring links -> Option AQuick Check:
Print missing after restoring links = bug [OK]
- Not printing node after restoring predecessor link
- Confusing when to update curr pointer
- Incorrectly modifying tree structure
Solution
Step 1: Identify when nodes are visited in Morris Traversal
Nodes are visited either whencurr.left === nullor after restoring the threaded link.Step 2: Collect values at both visit points
To collect all nodes in inorder, pushcurr.valboth whencurr.left === nulland after restoring links (before moving right).Final Answer:
Pushcurr.valboth whencurr.left === nulland after restoring links -> Option BQuick Check:
Collect values at both visits = correct inorder [OK]
- Collecting values only once misses nodes
- Collecting before moving left causes wrong order
- Ignoring second visit after restoring links
Ready to prove it?
Pass the test and this pattern moves unknown
