Challenge - 5 Problems
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code creating a binary tree node?
Consider the following Rust code that defines a binary tree node and creates a root node with value 10. What will be printed?
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn main() {
let root = TreeNode {
val: 10,
left: None,
right: None,
};
println!("{}", root.val);
}Attempts:
2 left
💡 Hint
Look at the value assigned to the root node's val field.
✗ Incorrect
The root node is created with val = 10, and printing root.val outputs 10.
❓ Predict Output
intermediateWhat is the output when accessing left child of a leaf node?
Given a binary tree node with no children, what will be the output of printing the left child?
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn main() {
let leaf = TreeNode {
val: 5,
left: None,
right: None,
};
match leaf.left {
Some(_) => println!("Has left child"),
None => println!("No left child"),
}
}Attempts:
2 left
💡 Hint
Check the value assigned to left field in leaf node.
✗ Incorrect
The left child is None, so the match arm prints 'No left child'.
🔧 Debug
advancedWhat error does this Rust code produce when creating a cyclic binary tree?
This code tries to create a binary tree node whose left child points back to itself. What error occurs?
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn main() {
let mut root = TreeNode {
val: 1,
left: None,
right: None,
};
root.left = Some(Box::new(root));
}Attempts:
2 left
💡 Hint
Consider ownership rules when assigning root inside itself.
✗ Incorrect
Assigning root inside root.left tries to move root while it is borrowed, causing a compile-time error.
🧠 Conceptual
advancedHow is a binary tree node typically represented in Rust?
Which of the following best describes the typical Rust struct definition for a binary tree node?
Attempts:
2 left
💡 Hint
Consider how to represent optional children in Rust safely.
✗ Incorrect
Option> allows for nullable children and heap allocation, which is typical in Rust binary trees.
🚀 Application
expertWhat is the output of this Rust code printing a simple binary tree structure?
Given the code below that creates a root node with a left child, what is the printed output?
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn print_tree(node: &Option<Box<TreeNode>>) {
match node {
Some(n) => {
print!("{} -> ", n.val);
print_tree(&n.left);
}
None => print!("null")
}
}
fn main() {
let left_child = TreeNode {
val: 5,
left: None,
right: None,
};
let root = TreeNode {
val: 10,
left: Some(Box::new(left_child)),
right: None,
};
print_tree(&Some(Box::new(root)));
}Attempts:
2 left
💡 Hint
Trace the recursive print_tree calls starting from root.
✗ Incorrect
The print_tree function prints the current node's val, then recursively prints left child, ending with null.
