Challenge - 5 Problems
Morris Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Morris Inorder Traversal on a Simple Tree
What is the output of the Morris inorder traversal on the following binary tree?
Tree structure:
2
/ \
1 3
Assume the traversal prints node values separated by spaces.
Tree structure:
2
/ \
1 3
Assume the traversal prints node values separated by spaces.
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } function morrisInorder(root: Node | null): void { let current = root; while (current !== null) { if (current.left === null) { process.stdout.write(current.val + ' '); current = current.right; } else { let pre = current.left; while (pre.right !== null && pre.right !== current) { pre = pre.right; } if (pre.right === null) { pre.right = current; current = current.left; } else { pre.right = null; process.stdout.write(current.val + ' '); current = current.right; } } } } const root = new Node(2); root.left = new Node(1); root.right = new Node(3); morrisInorder(root);
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Morris inorder traversal visits nodes in ascending order for BST. For the tree with root 2, left child 1, right child 3, the inorder sequence is 1 2 3.
🧠 Conceptual
intermediate1:30remaining
Understanding Thread Creation in Morris Traversal
In Morris inorder traversal, what is the purpose of creating a temporary thread (link) from the inorder predecessor to the current node?
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
✗ Incorrect
The temporary thread allows the traversal to return to the current node after finishing the left subtree without using stack or recursion. It is removed after use, so the tree remains unchanged.
🔧 Debug
advanced2:00remaining
Identify the Error in Modified Morris Traversal Code
What error will occur when running this modified Morris inorder traversal code snippet?
Code snippet:
let current = root;
while (current !== null) {
if (current.left !== null) {
let pre = current.left;
while (pre.right !== null && pre.right !== current) {
pre = pre.right;
}
if (pre.right === null) {
pre.right = current;
current = current.left;
} else {
pre.right = null;
console.log(current.val);
current = current.right;
}
} else {
console.log(current.val);
current = current.right;
}
}
Note: The condition in the first if is reversed from standard Morris traversal.
Code snippet:
let current = root;
while (current !== null) {
if (current.left !== null) {
let pre = current.left;
while (pre.right !== null && pre.right !== current) {
pre = pre.right;
}
if (pre.right === null) {
pre.right = current;
current = current.left;
} else {
pre.right = null;
console.log(current.val);
current = current.right;
}
} else {
console.log(current.val);
current = current.right;
}
}
Note: The condition in the first if is reversed from standard Morris traversal.
Attempts:
2 left
💡 Hint
Check what happens when current.left is null in this code.
✗ Incorrect
The code only creates threads when current.left is not null, but the condition is reversed, so when current.left is null, it skips thread creation and can get stuck in an infinite loop.
📝 Syntax
advanced1:30remaining
Identify Syntax Error in Morris Traversal Implementation
Which option contains a syntax error that prevents the Morris inorder traversal code from running?
Attempts:
2 left
💡 Hint
Look for missing semicolons or braces in the code.
✗ Incorrect
Option D is missing semicolons between statements inside the if block, causing syntax errors.
🚀 Application
expert2:00remaining
Number of Temporary Threads Created in Morris Traversal
Given a binary tree with 5 nodes arranged as:
4
/ \
2 5
/ \
1 3
How many temporary threads (links) will Morris inorder traversal create during the full traversal?
4
/ \
2 5
/ \
1 3
How many temporary threads (links) will Morris inorder traversal create during the full traversal?
Attempts:
2 left
💡 Hint
Count how many nodes have a left child and thus create a thread to their inorder predecessor.
✗ Incorrect
Threads are created for nodes with left children. Nodes 4 and 2 have left children, so threads are created for them. Node 2's left child 1 has no left child, so no thread. Total threads = 2 + 1 (for node 2's left subtree) = 3.