📖 Scenario: Imagine you have a family tree represented as a binary tree. You want to visit each family member in order from left to right without using extra memory like a stack or recursion.
🎯 Goal: Build a Java program that performs an inorder traversal of a binary tree using Morris Traversal technique, which uses no stack or recursion, and prints the node values in order.
📋 What You'll Learn
Create a binary tree with exact nodes and structure
Use a variable to track the current node
Implement Morris Traversal inorder logic without stack or recursion
Print the inorder traversal result as space-separated node values
💡 Why This Matters
🌍 Real World
Morris Traversal is useful when memory is limited and you want to traverse a binary tree without extra space for stack or recursion.
💼 Career
Understanding Morris Traversal helps in optimizing tree traversal algorithms and is valuable for software engineers working on memory-efficient applications.
Progress0 / 4 steps
1
Create the Binary Tree
Create a class called Node with integer data, and Node references left and right. Then create a binary tree with root node root having value 1, left child 2, right child 3, left child of 2 as 4, and right child of 2 as 5.
DSA Java
Hint
Define a Node class with data, left, and right. Then create nodes and link them exactly as described.
2
Add Current Node Variable
Inside the main method, create a variable called current and set it to root. This will track the node we are currently visiting.
DSA Java
Hint
Just assign current to root inside main.
3
Implement Morris Inorder Traversal Logic
Write a while loop that runs while current != null. Inside the loop, if current.left is null, print current.data followed by a space and move current to current.right. Otherwise, find the rightmost node in current.left subtree (predecessor). If predecessor's right is null, set it to current and move current to current.left. Else, set predecessor's right to null, print current.data followed by a space, and move current to current.right.
DSA Java
Hint
Use the Morris Traversal steps carefully: find predecessor, create thread, remove thread, and print nodes.
4
Print the Final Inorder Traversal Output
Add a System.out.println() after the while loop to print a newline for clean output.
DSA Java
Hint
Print a newline after the traversal to keep output clean.
Practice
(1/5)
1. What is the main advantage of Morris Traversal for inorder tree traversal compared to the traditional recursive or stack-based methods?
easy
A. It requires the tree to be balanced
B. It is faster than all other traversal methods
C. It only works on binary search trees
D. It uses no extra space for 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 recursion or stack which require O(h) space, where h is tree height.
Final Answer:
It uses no extra space for stack or recursion -> Option D
Quick Check:
Space complexity = O(1) [OK]
Hint: Morris uses threaded links to avoid stack [OK]
Common Mistakes:
Thinking Morris is faster in time complexity
Assuming it only works on BSTs
Believing it requires balanced trees
2. Which of the following code snippets correctly creates the temporary thread link in Morris Traversal inorder method?
easy
A. current.right = predecessor;
B. current.left = predecessor;
C. predecessor.right = current;
D. predecessor.left = current;
Solution
Step 1: Identify the thread link direction
In Morris Traversal, the right child of the inorder predecessor is linked to current node to create a thread.
Step 2: Match with code snippet
The correct assignment is predecessor.right = current; to create the temporary link.
Final Answer:
predecessor.right = current; -> Option C
Quick Check:
Thread link = predecessor.right = current [OK]
Hint: Thread link always sets predecessor.right to current [OK]
Common Mistakes:
Assigning current.left to predecessor instead
Using predecessor.left instead of right
Confusing current.right with predecessor.right
3. What is the output of the Morris inorder traversal on the following binary tree?
2
/ \
1 3
medium
A. [1, 2, 3]
B. [2, 1, 3]
C. [3, 2, 1]
D. [1, 3, 2]
Solution
Step 1: Perform inorder traversal manually
Inorder traversal visits left subtree, root, then right subtree. For this tree: left=1, root=2, right=3.
Step 2: List nodes in inorder
Nodes visited in order: 1, 2, 3.
Final Answer:
[1, 2, 3] -> Option A
Quick Check:
Inorder = left-root-right = [1, 2, 3] [OK]
Hint: Inorder = left, root, right sequence [OK]
Common Mistakes:
Confusing preorder or postorder with inorder
Mixing up left and right child order
Assuming Morris changes traversal order
4. Consider this buggy snippet from a Morris inorder traversal implementation:
while (current != null) {
if (current.left == null) {
System.out.print(current.val + " ");
current = current.right;
} else {
TreeNode predecessor = current.left;
while (predecessor.right != null) {
predecessor = predecessor.right;
}
predecessor.right = current;
current = current.left;
}
}
What is the main bug causing an infinite loop?
medium
A. Not printing current.val when left is not null
B. Not checking if predecessor.right == current before linking
C. Not moving current to current.right when left is null
D. Not initializing predecessor correctly
Solution
Step 1: Identify missing condition in inner loop
The inner while loop should stop if predecessor.right == current to avoid infinite loop.
Step 2: Explain infinite loop cause
Without checking predecessor.right == current, the loop never breaks and keeps moving right, causing infinite loop.
Final Answer:
Not checking if predecessor.right == current before linking -> Option B
Quick Check:
Check predecessor.right == current to break loop [OK]
Hint: Always check predecessor.right == current to avoid loops [OK]
Common Mistakes:
Ignoring thread existence check
Assuming printing happens in else block
Incorrect predecessor initialization
5. Given a binary tree where some nodes have only left children, how does Morris Traversal handle these nodes to produce the inorder sequence without extra space?
hard
A. It creates temporary right links from the rightmost node of left subtree back to current node
B. It skips nodes with only left children to avoid infinite loops
C. It uses recursion internally to handle left-only nodes
D. It converts the tree into a linked list before traversal
Solution
Step 1: Understand Morris Traversal threading
For nodes with left children, Morris Traversal finds the rightmost node in left subtree and links its right pointer to current node temporarily.
Step 2: Explain handling of left-only nodes
This threading allows traversal to return to current node after left subtree is processed, enabling inorder traversal without stack or recursion.
Final Answer:
It creates temporary right links from the rightmost node of left subtree back to current node -> Option A
Quick Check:
Threading rightmost left subtree node to current [OK]
Hint: Thread rightmost left child back to current node [OK]
Common Mistakes:
Thinking recursion is used internally
Assuming nodes are skipped
Believing tree is converted to linked list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.