Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Preorder Traversal on a Simple Binary Tree
What is the output of the preorder traversal (Root, Left, Right) of the following binary tree?
Tree structure:
1
/ \ 2 3
/
4
Tree structure:
1
/ \ 2 3
/
4
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn preorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
result.push(node.val);
preorder(&node.left, result);
preorder(&node.right, result);
}
}
fn main() {
let root = Some(Box::new(Node {
val: 1,
left: Some(Box::new(Node {
val: 2,
left: Some(Box::new(Node { val: 4, left: None, right: None })),
right: None,
})),
right: Some(Box::new(Node { val: 3, left: None, right: None })),
}));
let mut result = Vec::new();
preorder(&root, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember preorder traversal visits the root node first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in the order: root, left child, then right child. So starting at 1, then left subtree 2 and its left child 4, then right subtree 3.
❓ Predict Output
intermediatePreorder Traversal Output with Missing Right Child
What is the preorder traversal output of this binary tree?
Tree structure:
5
/ \ 6 null
/ \ 7 8
Tree structure:
5
/ \ 6 null
/ \ 7 8
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn preorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
result.push(node.val);
preorder(&node.left, result);
preorder(&node.right, result);
}
}
fn main() {
let root = Some(Box::new(Node {
val: 5,
left: Some(Box::new(Node {
val: 6,
left: Some(Box::new(Node { val: 7, left: None, right: None })),
right: Some(Box::new(Node { val: 8, left: None, right: None })),
})),
right: None,
}));
let mut result = Vec::new();
preorder(&root, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Preorder visits root first, then left subtree, then right subtree. Right child of root is missing here.
✗ Incorrect
Start at root 5, then left subtree 6, then 6's left child 7, then 6's right child 8. No right child of 5 to visit.
🔧 Debug
advancedIdentify the Error in Preorder Traversal Implementation
What error will this Rust code produce when running preorder traversal on a binary tree?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn preorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if root.is_some() {
let node = root.as_ref().unwrap();
result.push(node.val);
preorder(&node.left, result);
preorder(&node.right, result);
}
}
fn main() {
let root = Some(Box::new(Node {
val: 10,
left: None,
right: None,
}));
let mut result = Vec::new();
preorder(&root, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check how unwrap() is used on a reference to Option>.
✗ Incorrect
Calling unwrap() on a reference to Option> tries to move the Box out, which is not allowed. This causes a compilation error about moving out of borrowed content.
❓ Predict Output
advancedPreorder Traversal Output of a Larger Tree
What is the preorder traversal output of this binary tree?
Tree structure:
10
/ \ > 5 15
/ \ \ > 3 7 20
Tree structure:
10
/ \ > 5 15
/ \ \ > 3 7 20
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn preorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
result.push(node.val);
preorder(&node.left, result);
preorder(&node.right, result);
}
}
fn main() {
let root = Some(Box::new(Node {
val: 10,
left: Some(Box::new(Node {
val: 5,
left: Some(Box::new(Node { val: 3, left: None, right: None })),
right: Some(Box::new(Node { val: 7, left: None, right: None })),
})),
right: Some(Box::new(Node {
val: 15,
left: None,
right: Some(Box::new(Node { val: 20, left: None, right: None })),
})),
}));
let mut result = Vec::new();
preorder(&root, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Preorder visits root, then left subtree, then right subtree recursively.
✗ Incorrect
Start at root 10, then left subtree 5, then 5's left 3, 5's right 7, then right subtree 15, then 15's right 20.
🧠 Conceptual
expertNumber of Nodes Visited in Preorder Traversal
Given a binary tree with 7 nodes, how many nodes does the preorder traversal visit?
Attempts:
1 left
💡 Hint
Preorder traversal visits every node exactly once.
✗ Incorrect
Preorder traversal visits every node in the tree exactly once, so for 7 nodes, it visits 7 nodes.
