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 printed output of this Rust code creating a binary tree?
Consider this Rust code that manually creates a binary tree and prints its nodes in preorder (root, left, right). What will be the output?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn preorder(root: Option<Rc<RefCell<TreeNode>>>) { if let Some(node) = root { print!("{} ", node.borrow().val); preorder(node.borrow().left.clone()); preorder(node.borrow().right.clone()); } } fn main() { let root = TreeNode::new(10); let left = TreeNode::new(5); let right = TreeNode::new(15); let left_left = TreeNode::new(3); root.borrow_mut().left = Some(left.clone()); root.borrow_mut().right = Some(right.clone()); left.borrow_mut().left = Some(left_left.clone()); preorder(Some(root)); }
Attempts:
2 left
💡 Hint
Preorder traversal prints root first, then left subtree, then right subtree.
✗ Incorrect
The preorder traversal visits the root node first (10), then recursively visits the left subtree (5, then 3), then the right subtree (15). So the output is '10 5 3 15 '.
❓ Predict Output
intermediateWhat is the output after inserting nodes manually in this Rust binary tree?
Given this Rust code that manually inserts nodes into a binary tree, what is the output of the inorder traversal (left, root, right)?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn inorder(root: Option<Rc<RefCell<TreeNode>>>) { if let Some(node) = root { inorder(node.borrow().left.clone()); print!("{} ", node.borrow().val); inorder(node.borrow().right.clone()); } } fn main() { let root = TreeNode::new(20); let left = TreeNode::new(10); let right = TreeNode::new(30); let right_left = TreeNode::new(25); root.borrow_mut().left = Some(left.clone()); root.borrow_mut().right = Some(right.clone()); right.borrow_mut().left = Some(right_left.clone()); inorder(Some(root)); }
Attempts:
2 left
💡 Hint
Inorder traversal prints left subtree, then root, then right subtree.
✗ Incorrect
The inorder traversal visits left subtree (10), then root (20), then right subtree (25, then 30). So the output is '10 20 25 30 '.
🔧 Debug
advancedWhat error does this Rust code produce when creating a binary tree?
This Rust code tries to create a binary tree node but has a mistake. What error will it produce when compiled?
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
impl TreeNode {
fn new(val: i32) -> Box<TreeNode> {
Box::new(TreeNode { val, left: None, right: None })
}
}
fn main() {
let root = TreeNode::new(1);
root.left = Some(TreeNode::new(2));
}Attempts:
2 left
💡 Hint
Check if the variable is mutable before assigning to its fields.
✗ Incorrect
The variable `root` is not declared as mutable, so trying to assign to `root.left` causes a compiler error about mutability.
❓ Predict Output
advancedWhat is the output of this Rust code printing a binary tree level order?
This Rust code manually creates a binary tree and prints its nodes level by level. What is the output?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; use std::collections::VecDeque; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn level_order(root: Option<Rc<RefCell<TreeNode>>>) { if root.is_none() { return; } let mut queue = VecDeque::new(); queue.push_back(root.unwrap()); while !queue.is_empty() { let node = queue.pop_front().unwrap(); print!("{} ", node.borrow().val); if let Some(left) = node.borrow().left.clone() { queue.push_back(left); } if let Some(right) = node.borrow().right.clone() { queue.push_back(right); } } } fn main() { let root = TreeNode::new(1); let left = TreeNode::new(2); let right = TreeNode::new(3); let left_left = TreeNode::new(4); let right_right = TreeNode::new(5); root.borrow_mut().left = Some(left.clone()); root.borrow_mut().right = Some(right.clone()); left.borrow_mut().left = Some(left_left.clone()); right.borrow_mut().right = Some(right_right.clone()); level_order(Some(root)); }
Attempts:
2 left
💡 Hint
Level order traversal prints nodes level by level from left to right.
✗ Incorrect
The tree levels are: level 1: 1; level 2: 2, 3; level 3: 4, 5. So output is '1 2 3 4 5 '.
🧠 Conceptual
expertHow many nodes are in this manually created binary tree in Rust?
Given this Rust code manually creating a binary tree, how many nodes does the tree contain after all insertions?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { if let Some(node) = root { 1 + count_nodes(node.borrow().left.clone()) + count_nodes(node.borrow().right.clone()) } else { 0 } } fn main() { let root = TreeNode::new(7); let n1 = TreeNode::new(3); let n2 = TreeNode::new(9); let n3 = TreeNode::new(1); let n4 = TreeNode::new(5); let n5 = TreeNode::new(8); let n6 = TreeNode::new(10); root.borrow_mut().left = Some(n1.clone()); root.borrow_mut().right = Some(n2.clone()); n1.borrow_mut().left = Some(n3.clone()); n1.borrow_mut().right = Some(n4.clone()); n2.borrow_mut().left = Some(n5.clone()); n2.borrow_mut().right = Some(n6.clone()); let total = count_nodes(Some(root)); println!("{}", total); }
Attempts:
2 left
💡 Hint
Count all nodes including root and all children.
✗ Incorrect
The tree has root plus 6 other nodes connected as left and right children, total 7 nodes.
