What is the main advantage of Morris Traversal for inorder tree traversal?
Morris Traversal allows inorder traversal of a binary tree without using extra space for stack or recursion, by temporarily modifying the tree's structure.
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 helps to return to the current node after visiting its left subtree, enabling traversal without a stack or recursion.
Click to reveal answer
intermediate
What happens when the inorder predecessor's right pointer is NULL in Morris Traversal?
A temporary thread is created by setting the inorder predecessor's right pointer to the current node, then traversal moves to the left child.
Click to reveal answer
intermediate
What does Morris Traversal do when it encounters a thread (temporary link) during traversal?
It removes the thread by setting the inorder predecessor's right pointer back to NULL, visits the current node, and then moves to the right child.
Click to reveal answer
beginner
Why is Morris Traversal considered efficient in terms of space complexity?
Because it uses O(1) extra space by avoiding stack or recursion, modifying the tree temporarily and restoring it after traversal.
Click to reveal answer
What data structure does Morris Traversal avoid using for inorder traversal?
AStack
BQueue
CHeap
DLinked List
✗ Incorrect
Morris Traversal avoids using a stack or recursion by creating temporary threads in the tree.
In Morris Traversal, what is the inorder predecessor of a node?
ARightmost node in the left subtree
BLeftmost node in the right subtree
CParent node
DRoot node
✗ Incorrect
The inorder predecessor is the rightmost node in the left subtree of the current node.
What does Morris Traversal do when the inorder predecessor's right pointer is already pointing to the current node?
AMoves to the left child
BCreates a new thread
CRemoves the thread and visits the current node
DTerminates traversal
✗ Incorrect
When the thread exists, Morris Traversal removes it, visits the current node, and moves to the right child.
What is the space complexity of Morris Traversal?
AO(n)
BO(n log n)
CO(log n)
DO(1)
✗ Incorrect
Morris Traversal uses constant extra space by modifying the tree temporarily.
Which traversal order does Morris Traversal implement without stack or recursion?
ALevel order
BInorder
CPostorder
DPreorder
✗ Incorrect
Morris Traversal is primarily used for inorder traversal without stack or recursion.
Explain step-by-step how Morris Traversal performs inorder traversal without using a stack or recursion.
Think about how the traversal remembers where to return after left subtree.
You got /5 concepts.
Describe the role of temporary threads in Morris Traversal and how they help achieve O(1) space complexity.
Consider how the traversal simulates the call stack using tree pointers.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using Morris Traversal for inorder tree traversal compared to the traditional recursive or stack-based methods?
easy
A. It sorts the tree nodes during traversal
B. It is faster because it uses multiple threads
C. It requires the tree to be a binary search tree
D. It uses no extra space for stack or recursion, only modifies tree temporarily
Solution
Step 1: Understand Morris Traversal space usage
Morris Traversal does not use recursion or an explicit stack, instead it temporarily modifies the tree's right pointers to traverse.
Step 2: Compare with traditional methods
Traditional inorder traversal uses recursion or a stack, which requires extra memory proportional to tree height.
Final Answer:
It uses no extra space for stack or recursion, only modifies tree temporarily -> Option D
Quick Check:
Space efficiency = No extra stack [OK]
Hint: Morris uses tree links, no stack or recursion needed [OK]
Common Mistakes:
Thinking Morris sorts nodes
Assuming it needs a binary search tree
Believing it uses threads or parallelism
2. Which of the following code snippets correctly initializes the current pointer for Morris inorder traversal in C++?
easy
A. Node current = root;
B. Node* current = root;
C. Node* current = new Node(root);
D. Node current = *root;
Solution
Step 1: Understand pointer usage in C++ for tree traversal
We need a pointer to traverse nodes, so current should be a pointer to Node initialized to root pointer.
Step 2: Analyze options
Node* current = root; correctly declares a pointer and assigns root. Options B and D declare objects, not pointers. Node* current = new Node(root); incorrectly uses new with root pointer.
Final Answer:
Node* current = root; -> Option B
Quick Check:
Pointer initialization = Node* current = root [OK]
Hint: Use pointer Node* current = root for traversal [OK]
Common Mistakes:
Declaring current as object instead of pointer
Using new operator incorrectly
Dereferencing root without pointer
3. Consider the following C++ code snippet implementing Morris inorder traversal on a binary tree with nodes 1, 2, 3 where 1 is root, 2 is left child, and 3 is right child. What is the output printed?
Missing check pre->right != current causes loop to never break when pre->right points back to current, causing infinite loop.
Final Answer:
The inner while loop misses checking pre->right != current causing infinite loop -> Option A
Quick Check:
Loop condition must check pre->right != current [OK]
Hint: Check inner loop condition includes pre->right != current [OK]
Common Mistakes:
Ignoring second condition in inner loop
Forgetting to update current after printing
Not handling empty tree
5. You want to modify Morris inorder traversal to count the number of nodes in a binary tree without using extra space or recursion. Which modification is correct?
void morrisCount(Node* root) {
int count = 0;
Node* current = root;
while (current != nullptr) {
if (current->left == nullptr) {
count++;
current = current->right;
} else {
Node* pre = current->left;
while (pre->right != nullptr && pre->right != current) {
pre = pre->right;
}
if (pre->right == nullptr) {
pre->right = current;
current = current->left;
} else {
pre->right = nullptr;
count++;
current = current->right;
}
}
}
std::cout << count << std::endl;
}
hard
A. This code correctly counts nodes using Morris traversal without extra space
B. Counting should be done only when pre->right is nullptr
C. Count variable must be global to work correctly
D. Morris traversal cannot be used to count nodes without recursion
Solution
Step 1: Understand counting during Morris traversal
Count nodes when visiting them: when left is nullptr or after restoring links (pre->right == current).
Step 2: Verify code increments count correctly
Code increments count in both cases correctly, so it counts all nodes without extra space or recursion.
Final Answer:
This code correctly counts nodes using Morris traversal without extra space -> Option A
Quick Check:
Count increments on node visit = correct [OK]
Hint: Increment count on node visit before moving right [OK]
Common Mistakes:
Counting only once per node visit
Making count global unnecessarily
Assuming Morris can't count nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.