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 Morris inorder traversal on the following binary tree?
Tree structure:
2
/ \
1 3
Assume the traversal prints node values in order.
Tree structure:
2
/ \
1 3
Assume the traversal prints node values in order.
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn morris_inorder(root: &mut Option<Box<Node>>) -> Vec<i32> {
let mut result = Vec::new();
let mut current = root.as_mut().map(|b| &mut **b);
while let Some(node) = current {
if node.left.is_none() {
result.push(node.val);
current = node.right.as_mut().map(|b| &mut **b);
} else {
let mut pre = node.left.as_mut().map(|b| &mut **b).unwrap();
while pre.right.is_some() && pre.right.as_ref().unwrap().val != node.val {
pre = pre.right.as_mut().map(|b| &mut **b).unwrap();
}
if pre.right.is_none() {
pre.right = Some(Box::new(Node { val: node.val, left: None, right: None }));
current = node.left.as_mut().map(|b| &mut **b);
} else {
pre.right = None;
result.push(node.val);
current = node.right.as_mut().map(|b| &mut **b);
}
}
}
result
}
fn main() {
let mut root = Some(Box::new(Node {
val: 2,
left: Some(Box::new(Node { val: 1, left: None, right: None })),
right: Some(Box::new(Node { val: 3, left: None, right: None })),
}));
let output = morris_inorder(&mut root);
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Morris inorder traversal visits nodes in ascending order for a BST. The tree has nodes 1, 2, 3 with 2 as root, so output is [1, 2, 3].
🧠 Conceptual
intermediateWhy Morris Traversal uses threading?
Why does Morris inorder traversal create temporary links (threads) in the binary tree during traversal?
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
✗ Incorrect
Morris traversal creates temporary threads to the inorder successor to avoid recursion or stack, enabling O(1) space traversal.
🔧 Debug
advancedIdentify the error in this Morris traversal snippet
What error will this Rust code cause during Morris inorder traversal?
Code snippet:
let mut pre = node.left.as_mut().unwrap();
while pre.right.is_some() && pre.right.as_ref().unwrap().val != node.val {
pre = pre.right.as_mut().unwrap();
}
if pre.right.is_none() {
pre.right = Some(Box::new(Node { val: node.val, left: None, right: None }));
current = node.left.as_mut();
} else {
pre.right = None;
current = node.right.as_mut();
}
Code snippet:
let mut pre = node.left.as_mut().unwrap();
while pre.right.is_some() && pre.right.as_ref().unwrap().val != node.val {
pre = pre.right.as_mut().unwrap();
}
if pre.right.is_none() {
pre.right = Some(Box::new(Node { val: node.val, left: None, right: None }));
current = node.left.as_mut();
} else {
pre.right = None;
current = node.right.as_mut();
}
Attempts:
2 left
💡 Hint
Consider what happens if the thread is not removed properly.
✗ Incorrect
If the thread points back to the node and is not removed, the loop repeats infinitely causing runtime hang.
🚀 Application
advancedMorris Traversal on a skewed tree
Given a right-skewed binary tree with nodes 1 -> 2 -> 3 -> 4 (each node has only right child), what is the output of Morris inorder traversal?
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, node, then right subtree. Here left subtree is always empty.
✗ Incorrect
Since all nodes only have right children, inorder traversal visits nodes in ascending order: 1, 2, 3, 4.
❓ Predict Output
expertOutput of Morris Traversal on complex tree with duplicates
What is the output of Morris inorder traversal on this tree?
Tree structure:
4
/ \
2 5
/ \ \
2 3 5
Assume traversal prints node values in order.
Tree structure:
4
/ \
2 5
/ \ \
2 3 5
Assume traversal prints node values in order.
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn morris_inorder(root: &mut Option<Box<Node>>) -> Vec<i32> {
let mut result = Vec::new();
let mut current = root.as_mut().map(|b| &mut **b);
while let Some(node) = current {
if node.left.is_none() {
result.push(node.val);
current = node.right.as_mut().map(|b| &mut **b);
} else {
let mut pre = node.left.as_mut().map(|b| &mut **b).unwrap();
while pre.right.is_some() && pre.right.as_ref().unwrap().val != node.val {
pre = pre.right.as_mut().map(|b| &mut **b).unwrap();
}
if pre.right.is_none() {
pre.right = Some(Box::new(Node { val: node.val, left: None, right: None }));
current = node.left.as_mut().map(|b| &mut **b);
} else {
pre.right = None;
result.push(node.val);
current = node.right.as_mut().map(|b| &mut **b);
}
}
}
result
}
fn main() {
let mut root = Some(Box::new(Node {
val: 4,
left: Some(Box::new(Node {
val: 2,
left: Some(Box::new(Node { val: 2, left: None, right: None })),
right: Some(Box::new(Node { val: 3, left: None, right: None })),
})),
right: Some(Box::new(Node {
val: 5,
left: None,
right: Some(Box::new(Node { val: 5, left: None, right: None })),
})),
}));
let output = morris_inorder(&mut root);
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, node, then right subtree, including duplicates in order.
✗ Incorrect
The traversal visits left subtree nodes 2, 2, 3, then root 4, then right subtree 5, 5 in order.
