What is the main advantage of Morris Traversal for inorder tree traversal?
Morris Traversal uses no extra space like stack or recursion. It achieves inorder traversal with O(1) space by temporarily modifying the tree's right pointers.
Click to reveal answer
intermediate
In Morris Traversal, what is the purpose of creating a temporary thread (link) from the inorder predecessor to the current node?
The temporary thread allows returning to the current node after visiting its left subtree without using a stack or recursion.
Click to reveal answer
intermediate
What happens when the inorder predecessor's right pointer is already pointing to the current node in Morris Traversal?
It means the left subtree has been fully visited. The thread is removed by setting the right pointer back to null, and the current node is processed.
Click to reveal answer
beginner
Why does Morris Traversal restore the tree structure after traversal?
Because it temporarily changes right pointers to create threads, it must restore them to null to keep the original tree unchanged after traversal.
Click to reveal answer
beginner
Which traversal order does Morris Traversal implement without stack or recursion?
Morris Traversal is commonly used for inorder traversal of a binary tree without stack or recursion.
Click to reveal answer
What is the space complexity of Morris Traversal for inorder traversal?
AO(log n)
BO(1)
CO(n)
DO(n log n)
✗ Incorrect
Morris Traversal uses no stack or recursion, only modifies pointers temporarily, so space complexity is O(1).
In Morris Traversal, what do you do if the current node has no left child?
AStop traversal
BCreate a thread to the current node
CMove to left child
DPrint current node and move to right child
✗ Incorrect
If no left child, process current node and move to right child directly.
How does Morris Traversal find the inorder predecessor of a node?
AGo to left child, then keep moving right until right child is null or current node
BGo to right child, then keep moving left
CUse a stack to track nodes
DUse recursion
✗ Incorrect
Inorder predecessor is the rightmost node in the left subtree.
What is the key step to avoid infinite loops in Morris Traversal?
AStop traversal after visiting root
BUse a stack to track nodes
CRemove the temporary thread after visiting left subtree
DUse recursion
✗ Incorrect
Removing the temporary thread prevents revisiting the same nodes endlessly.
Which of these is NOT true about Morris Traversal?
AIt uses recursion
BIt modifies the tree temporarily
CIt has O(1) space complexity
DIt restores the tree after traversal
✗ Incorrect
Morris Traversal does not use recursion; it uses pointer manipulation instead.
Explain step-by-step how Morris Traversal performs inorder traversal without using a stack or recursion.
Think about how temporary links help return to nodes after left subtree.
You got /6 concepts.
Describe why Morris Traversal is useful compared to traditional inorder traversal methods.
Focus on space usage and tree structure preservation.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of Morris Traversal for inorder tree traversal compared to traditional methods?
easy
A. It only works on binary search trees
B. It is faster than all other traversal methods
C. It requires a queue to store nodes
D. It uses no extra space like stack or recursion
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 D
Quick Check:
Morris Traversal = O(1) space [OK]
Hint: Morris Traversal avoids stack and recursion for space savings [OK]
Common Mistakes:
Thinking Morris Traversal is faster in time complexity
Assuming it only works on binary search trees
Believing it uses extra data structures like queues
2. Which of the following is the correct way to find the inorder predecessor of a node curr in Morris Traversal?
easy
A. Move to the left child of curr and then keep moving right until right child is null or curr
B. Move to the right child of curr until null
C. Move to the left child of curr only once
D. Move to the right child of curr and then keep moving left until left child is null
Solution
Step 1: Identify inorder predecessor in Morris Traversal
The inorder predecessor is the rightmost node in the left subtree of curr.
Step 2: Traverse left subtree then rightmost path
Start at curr.left, move right repeatedly until right child is null or points back to curr.
Final Answer:
Move to the left child of curr and then keep moving right until right child is null or curr -> Option A
Quick Check:
Inorder predecessor = left subtree's rightmost node [OK]
Hint: Inorder predecessor = left child then rightmost path [OK]
Common Mistakes:
Moving right child first instead of left
Stopping after one left move without going right
Confusing predecessor with successor
3. What will be the output of Morris inorder traversal on the following binary tree?
2
/ \
1 3
Assuming the traversal prints node values in order.
medium
A. [1, 3, 2]
B. [2, 1, 3]
C. [1, 2, 3]
D. [3, 2, 1]
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 C
Quick Check:
Inorder = left, root, right = [1, 2, 3] [OK]
Hint: Inorder prints left, root, right nodes [OK]
Common Mistakes:
Confusing preorder or postorder with inorder
Mixing node visit order
Assuming traversal prints in reverse
4. Consider this snippet of Morris Traversal code:
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?
medium
A. It misses printing the current node after restoring links
B. It never prints the current node's value
C. It creates infinite loop by not updating curr
D. It incorrectly sets pred.right to curr.left
Solution
Step 1: Check when node values are printed
Values are printed only when curr.left === null, but after restoring links, printing is missing.
Step 2: Identify missing print after restoring links
After setting pred.right = null, the code moves curr = curr.right without printing curr.val.
Final Answer:
It misses printing the current node after restoring links -> Option A
Quick Check:
Print missing after restoring links = bug [OK]
Hint: Print node after restoring links to avoid missing values [OK]
Common Mistakes:
Not printing node after restoring predecessor link
Confusing when to update curr pointer
Incorrectly modifying tree structure
5. You want to modify Morris Traversal inorder to collect node values in an array instead of printing. Which change is necessary to correctly collect all nodes in order?
hard
A. Push curr.val only when curr.left === null
B. Push curr.val both when curr.left === null and after restoring links
C. Push curr.val only after restoring links
D. Push curr.val before moving to left child
Solution
Step 1: Identify when nodes are visited in Morris Traversal
Nodes are visited either when curr.left === null or after restoring the threaded link.
Step 2: Collect values at both visit points
To collect all nodes in inorder, push curr.val both when curr.left === null and after restoring links (before moving right).
Final Answer:
Push curr.val both when curr.left === null and after restoring links -> Option B
Quick Check:
Collect values at both visits = correct inorder [OK]
Hint: Collect node values at both visits for full inorder [OK]
Common Mistakes:
Collecting values only once misses nodes
Collecting before moving left causes wrong order
Ignoring second visit after restoring links
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.