Challenge - 5 Problems
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of counting nodes in this binary tree?
Consider the following Rust code that counts nodes in a binary tree. What is the printed 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 count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { match root { None => 0, Some(node) => { 1 + count_nodes(node.borrow().left.clone()) + count_nodes(node.borrow().right.clone()) } } } fn main() { let root = TreeNode::new(1); let left = TreeNode::new(2); let right = TreeNode::new(3); let left_left = TreeNode::new(4); root.borrow_mut().left = Some(left.clone()); root.borrow_mut().right = Some(right.clone()); left.borrow_mut().left = Some(left_left.clone()); let total = count_nodes(Some(root)); println!("Total nodes: {}", total); }
Attempts:
2 left
💡 Hint
Count each node starting from root, including all children.
✗ Incorrect
The tree has nodes with values 1, 2, 3, and 4. Counting all nodes gives 4.
❓ Predict Output
intermediateWhat is the output after adding a right child to the left node?
Given the Rust code below, what will be the output after adding a right child with value 5 to the left node?
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 { match root { None => 0, Some(node) => { 1 + count_nodes(node.borrow().left.clone()) + count_nodes(node.borrow().right.clone()) } } } fn main() { let root = TreeNode::new(1); let left = TreeNode::new(2); let right = TreeNode::new(3); let left_left = TreeNode::new(4); let left_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()); left.borrow_mut().right = Some(left_right.clone()); let total = count_nodes(Some(root)); println!("Total nodes: {}", total); }
Attempts:
2 left
💡 Hint
Count all nodes including the new right child of the left node.
✗ Incorrect
The tree now has nodes 1, 2, 3, 4, and 5. Total nodes counted is 5.
🔧 Debug
advancedWhat error occurs when counting nodes with this broken Rust code?
This Rust code attempts to count nodes but has a bug. What error will it cause when run?
DSA Rust
use std::rc::Rc;
use std::cell::RefCell;
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 root.is_none() {
return 0;
}
let node = root.unwrap();
1 + count_nodes(node.borrow().left.clone()) + count_nodes(node.borrow().right.clone())
}
fn main() {
let root = TreeNode::new(1);
let left = TreeNode::new(2);
root.borrow_mut().left = Some(left);
let total = count_nodes(Some(root));
println!("Total nodes: {}", total);
}Attempts:
2 left
💡 Hint
Check how unwrap() is used on Option inside count_nodes.
✗ Incorrect
Using unwrap() on Option inside count_nodes moves the value, which is not allowed because it borrows the node. This causes a compilation error.
🧠 Conceptual
advancedWhy use Rc> for binary trees in Rust?
In Rust, why do we often use Rc> to represent nodes in a binary tree?
Attempts:
2 left
💡 Hint
Think about ownership and mutability in Rust's strict rules.
✗ Incorrect
Rc allows multiple owners of the same data, and RefCell allows mutable access checked at runtime. Together they enable shared mutable trees.
🚀 Application
expertWhat is the total node count after this sequence of insertions?
Starting with an empty binary tree, the following insertions happen in order: 10 (root), 5 (left child of 10), 15 (right child of 10), 3 (left child of 5), 7 (right child of 5), 12 (left child of 15). What is the total number of nodes in the tree?
Attempts:
2 left
💡 Hint
Count each inserted node carefully, including root and all children.
✗ Incorrect
The nodes inserted are 10, 5, 15, 3, 7, and 12, totaling 6 nodes.
