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 Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func morrisInorder(root *Node) { current := root for current != nil { if current.Left == nil { fmt.Print(current.Val, " ") current = current.Right } else { pre := current.Left for pre.Right != nil && pre.Right != current { pre = pre.Right } if pre.Right == nil { pre.Right = current current = current.Left } else { pre.Right = nil fmt.Print(current.Val, " ") current = current.Right } } } } func main() { root := &Node{Val: 2} root.Left = &Node{Val: 1} root.Right = &Node{Val: 3} morrisInorder(root) }
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Morris inorder traversal visits nodes in ascending order for a BST. The tree nodes are 1, 2, 3. So output is '1 2 3 '.
❓ Predict Output
intermediate2:00remaining
Output of Morris Inorder Traversal on a Tree with Only Left Children
What is the output of the Morris inorder traversal on this tree?
Tree structure:
4
/
3
/
2
/
1
Assume the traversal prints node values separated by spaces.
Tree structure:
4
/
3
/
2
/
1
Assume the traversal prints node values separated by spaces.
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func morrisInorder(root *Node) { current := root for current != nil { if current.Left == nil { fmt.Print(current.Val, " ") current = current.Right } else { pre := current.Left for pre.Right != nil && pre.Right != current { pre = pre.Right } if pre.Right == nil { pre.Right = current current = current.Left } else { pre.Right = nil fmt.Print(current.Val, " ") current = current.Right } } } } func main() { root := &Node{Val: 4} root.Left = &Node{Val: 3} root.Left.Left = &Node{Val: 2} root.Left.Left.Left = &Node{Val: 1} morrisInorder(root) }
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes from smallest to largest in this left-skewed tree.
✗ Incorrect
The inorder traversal visits nodes from leftmost (1) up to root (4) in ascending order.
🔧 Debug
advanced3:00remaining
Identify the Bug in Morris Traversal Code
The following Go code attempts Morris inorder traversal but produces incorrect output. What is the bug?
DSA Go
func morrisInorder(root *Node) {
current := root
for current != nil {
if current.Left == nil {
fmt.Print(current.Val, " ")
current = current.Right
} else {
pre := current.Left
for pre.Right != nil {
pre = pre.Right
}
if pre.Right == nil {
pre.Right = current
current = current.Left
} else {
pre.Right = nil
fmt.Print(current.Val, " ")
current = current.Right
}
}
}
}Attempts:
2 left
💡 Hint
Check the condition inside the inner while loop that finds the predecessor.
✗ Incorrect
The inner loop must check pre.Right != nil AND pre.Right != current to avoid infinite loops and correctly restore tree structure.
🧠 Conceptual
advanced1:30remaining
Why Morris Traversal Uses Threaded Links
Why does Morris inorder traversal create temporary links (threads) from the inorder predecessor to the current node?
Attempts:
2 left
💡 Hint
Think about how recursion or stack remembers the path back to a node.
✗ Incorrect
Morris traversal uses threads to remember the return path without extra memory, enabling O(1) space traversal.
🚀 Application
expert2:30remaining
Count Nodes Using Morris Traversal Without Extra Space
Using Morris inorder traversal, how can you count the total number of nodes in a binary tree without using recursion or a stack?
Attempts:
2 left
💡 Hint
Each node is visited exactly once in inorder traversal.
✗ Incorrect
In Morris traversal, each node is printed exactly once when visited. Counting these visits gives total nodes.