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
/* Tree structure: 2 / \ 1 3 */ struct Node { int data; struct Node* left; struct Node* right; }; void morrisInorder(struct Node* root) { struct Node* current = root; while (current != NULL) { if (current->left == NULL) { printf("%d ", current->data); current = current->right; } else { struct 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; printf("%d ", current->data); current = current->right; } } } } int main() { struct Node n1 = {1, NULL, NULL}; struct Node n3 = {3, NULL, NULL}; struct Node n2 = {2, &n1, &n3}; morrisInorder(&n2); return 0; }
Attempts:
2 left
💡 Hint
Remember that inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Morris traversal visits nodes in inorder without stack or recursion by temporarily modifying the tree. For this tree, inorder is 1, 2, 3.
❓ Predict Output
intermediateOutput of Morris Traversal on a Tree with Only Left Children
What is the output of the Morris inorder traversal on this tree?
DSA C
/* Tree structure: 3 / 2 / 1 */ struct Node { int data; struct Node* left; struct Node* right; }; void morrisInorder(struct Node* root) { struct Node* current = root; while (current != NULL) { if (current->left == NULL) { printf("%d ", current->data); current = current->right; } else { struct 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; printf("%d ", current->data); current = current->right; } } } } int main() { struct Node n1 = {1, NULL, NULL}; struct Node n2 = {2, &n1, NULL}; struct Node n3 = {3, &n2, NULL}; morrisInorder(&n3); return 0; }
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes from smallest to largest in a BST.
✗ Incorrect
The inorder traversal visits nodes in ascending order: 1, 2, 3.
🔧 Debug
advancedIdentify the Bug in Morris Traversal Code
What error will this Morris traversal code produce when run on a binary tree?
DSA C
void morrisInorder(struct Node* root) {
struct Node* current = root;
while (current != NULL) {
if (current->left == NULL) {
printf("%d ", current->data);
current = current->right;
} else {
struct 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;
printf("%d ", current->data);
current = current->right;
}
}
}
}Attempts:
2 left
💡 Hint
Check the inner while loop condition that finds the predecessor node.
✗ Incorrect
The inner while loop misses the check for pre->right != current, causing an infinite loop when the thread is already set.
🧠 Conceptual
advancedWhy Morris Traversal Uses Threaded Binary Tree Concept
Why does Morris inorder traversal temporarily modify the tree by creating threads?
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
✗ Incorrect
Morris traversal creates temporary links (threads) from a node's inorder predecessor back to the node to traverse without stack or recursion.
🚀 Application
expertNumber of Steps in Morris Traversal for a Complete Binary Tree
For a complete binary tree with n nodes, how many times is the inner while loop executed in Morris inorder traversal?
Attempts:
2 left
💡 Hint
Consider how many edges exist in a tree and how threads relate to edges.
✗ Incorrect
Each edge in the tree is used to create at most one thread, so the inner loop runs at most n-1 times for n nodes.
