Challenge - 5 Problems
Morris Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Morris Inorder Traversal on a Simple Tree
What is the output of the following Morris inorder traversal code on the given binary tree?
DSA C#
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } Node root = new Node(2); root.left = new Node(1); root.right = new Node(3); void MorrisInorder(Node root) { Node current = root; while (current != null) { if (current.left == null) { System.Console.Write(current.val + " "); current = current.right; } else { Node 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; System.Console.Write(current.val + " "); current = current.right; } } } } MorrisInorder(root);
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Morris traversal visits nodes in inorder without stack by temporarily modifying right pointers of predecessor nodes. For this tree, inorder is 1, 2, 3.
❓ Predict Output
intermediateOutput of Morris Traversal on Tree with Left Subtree Only
What will be printed by the Morris inorder traversal on this tree?
DSA C#
Node root = new Node(4); root.left = new Node(3); root.left.left = new Node(2); root.left.left.left = new Node(1); MorrisInorder(root);
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes from smallest to largest in BST.
✗ Incorrect
The traversal visits leftmost node first, then up the tree, printing 1 2 3 4.
🔧 Debug
advancedIdentify the Error in Modified Morris Traversal Code
What error will this modified Morris traversal code produce?
DSA C#
void MorrisInorder(Node root) {
Node current = root;
while (current != null) {
if (current.left == null) {
System.Console.Write(current.val + " ");
current = current.right;
} else {
Node pre = current.left;
while (pre.right != null) {
pre = pre.right;
}
if (pre.right == null) {
pre.right = current;
current = current.left;
} else {
pre.right = null;
System.Console.Write(current.val + " ");
current = current.right;
}
}
}
}
MorrisInorder(root);Attempts:
2 left
💡 Hint
Check the inner while loop condition for predecessor traversal.
✗ Incorrect
The inner while loop misses the check pre.right != current, causing infinite loop when pre.right points back to current.
🧠 Conceptual
advancedWhy Morris Traversal Uses Threaded Links Temporarily?
Why does Morris inorder traversal temporarily modify the tree by creating threaded links?
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
✗ Incorrect
Morris traversal creates temporary links from predecessor to current node to return after left subtree without stack or recursion.
🚀 Application
expertNumber of Steps in Morris Inorder Traversal for Complete Binary Tree
For a complete binary tree with n nodes, how many times is the inner while loop (finding predecessor) executed in Morris inorder traversal?
Attempts:
2 left
💡 Hint
Each edge is visited at most twice in Morris traversal.
✗ Incorrect
Each edge is traversed twice: once to create threaded link and once to remove it, so inner loop runs at most 2n times.
