Challenge - 5 Problems
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of balance check on a simple balanced tree
What is the output of the following Rust code that checks if a binary tree is balanced?
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 is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool { fn height(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 { match node { None => 0, Some(n) => { let left_height = height(&n.borrow().left); if left_height == -1 { return -1; } let right_height = height(&n.borrow().right); if right_height == -1 { return -1; } if (left_height - right_height).abs() > 1 { return -1; } 1 + left_height.max(right_height) } } } height(&root) != -1 } fn main() { let root = TreeNode::new(1); root.borrow_mut().left = Some(TreeNode::new(2)); root.borrow_mut().right = Some(TreeNode::new(3)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(4)); println!("{}", is_balanced(Some(root))); }
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees at each node.
✗ Incorrect
The tree is balanced because for every node, the height difference between left and right subtrees is at most 1.
The function returns true.
❓ Predict Output
intermediateOutput of balance check on an unbalanced tree
What is the output of the following Rust code that checks if a binary tree is balanced?
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 is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool { fn height(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 { match node { None => 0, Some(n) => { let left_height = height(&n.borrow().left); if left_height == -1 { return -1; } let right_height = height(&n.borrow().right); if right_height == -1 { return -1; } if (left_height - right_height).abs() > 1 { return -1; } 1 + left_height.max(right_height) } } } height(&root) != -1 } fn main() { let root = TreeNode::new(1); root.borrow_mut().left = Some(TreeNode::new(2)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(3)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(4)); println!("{}", is_balanced(Some(root))); }
Attempts:
2 left
💡 Hint
Check the height difference at the root node.
✗ Incorrect
The tree is unbalanced because the left subtree is much deeper than the right (which is empty).
The function returns false.
🧠 Conceptual
advancedUnderstanding the height function in balance check
In the balance check function, why does the height function return -1?
Attempts:
2 left
💡 Hint
Think about what a special return value might mean in a recursive check.
✗ Incorrect
The height function returns -1 as a special signal that the subtree is unbalanced.
🔧 Debug
advancedIdentify the bug causing incorrect balance result
Given this Rust code snippet for balance check, which option explains why it incorrectly returns true for an unbalanced tree?
DSA Rust
fn height(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
match node {
None => 0,
Some(n) => {
let left_height = height(&n.borrow().left);
let right_height = height(&n.borrow().right);
if (left_height - right_height).abs() > 1 {
return 0; // Bug here
}
1 + left_height.max(right_height)
}
}
}Attempts:
2 left
💡 Hint
What special value is used to indicate unbalance?
✗ Incorrect
Returning 0 instead of -1 fails to propagate the unbalanced signal, so the main function thinks the tree is balanced.
🚀 Application
expertNumber of nodes visited during balance check
For a complete binary tree with 7 nodes, how many nodes does the balance check function visit in the worst case?
Attempts:
2 left
💡 Hint
The function visits each node once to compute height.
✗ Incorrect
The function visits every node once to compute heights and check balance.
