Challenge - 5 Problems
LCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LCA function for given nodes
What is the output of the Rust function that finds the Lowest Common Ancestor (LCA) of nodes 5 and 1 in the given binary tree?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug, PartialEq, Eq)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } fn lowest_common_ancestor(root: Option<Rc<RefCell<TreeNode>>>, p: i32, q: i32) -> Option<i32> { fn helper(node: &Option<Rc<RefCell<TreeNode>>>, p: i32, q: i32) -> Option<Rc<RefCell<TreeNode>>> { if let Some(n) = node { let n_borrow = n.borrow(); if n_borrow.val == p || n_borrow.val == q { return Some(Rc::clone(n)); } let left = helper(&n_borrow.left, p, q); let right = helper(&n_borrow.right, p, q); if left.is_some() && right.is_some() { return Some(Rc::clone(n)); } if left.is_some() { return left; } right } else { None } } helper(&root, p, q).map(|node| node.borrow().val) } fn main() { let node5 = Rc::new(RefCell::new(TreeNode { val: 5, left: None, right: None })); let node1 = Rc::new(RefCell::new(TreeNode { val: 1, left: None, right: None })); let node6 = Rc::new(RefCell::new(TreeNode { val: 6, left: None, right: None })); let node2 = Rc::new(RefCell::new(TreeNode { val: 2, left: None, right: None })); let node0 = Rc::new(RefCell::new(TreeNode { val: 0, left: None, right: None })); let node8 = Rc::new(RefCell::new(TreeNode { val: 8, left: None, right: None })); let node7 = Rc::new(RefCell::new(TreeNode { val: 7, left: None, right: None })); let node4 = Rc::new(RefCell::new(TreeNode { val: 4, left: None, right: None })); node5.borrow_mut().left = Some(Rc::clone(&node6)); node5.borrow_mut().right = Some(Rc::clone(&node2)); node2.borrow_mut().left = Some(Rc::clone(&node7)); node2.borrow_mut().right = Some(Rc::clone(&node4)); node1.borrow_mut().left = Some(Rc::clone(&node0)); node1.borrow_mut().right = Some(Rc::clone(&node8)); let root = Some(Rc::clone(&node5)); let lca = lowest_common_ancestor(root, 5, 1); println!("{}", lca.unwrap()); }
Attempts:
2 left
💡 Hint
The LCA of a node with itself or its ancestor is the ancestor node.
✗ Incorrect
The nodes 5 and 1 have 5 as their lowest common ancestor because 5 is the root and ancestor of 1.
❓ Predict Output
intermediateOutput of LCA function for nodes 7 and 4
What is the output of the Rust LCA function when finding the lowest common ancestor of nodes 7 and 4 in the same binary tree?
DSA Rust
/* Same tree setup as previous challenge */ fn main() { // Tree nodes and structure as before let node5 = Rc::new(RefCell::new(TreeNode { val: 5, left: None, right: None })); let node1 = Rc::new(RefCell::new(TreeNode { val: 1, left: None, right: None })); let node6 = Rc::new(RefCell::new(TreeNode { val: 6, left: None, right: None })); let node2 = Rc::new(RefCell::new(TreeNode { val: 2, left: None, right: None })); let node0 = Rc::new(RefCell::new(TreeNode { val: 0, left: None, right: None })); let node8 = Rc::new(RefCell::new(TreeNode { val: 8, left: None, right: None })); let node7 = Rc::new(RefCell::new(TreeNode { val: 7, left: None, right: None })); let node4 = Rc::new(RefCell::new(TreeNode { val: 4, left: None, right: None })); node5.borrow_mut().left = Some(Rc::clone(&node6)); node5.borrow_mut().right = Some(Rc::clone(&node2)); node2.borrow_mut().left = Some(Rc::clone(&node7)); node2.borrow_mut().right = Some(Rc::clone(&node4)); node1.borrow_mut().left = Some(Rc::clone(&node0)); node1.borrow_mut().right = Some(Rc::clone(&node8)); let root = Some(Rc::clone(&node5)); let lca = lowest_common_ancestor(root, 7, 4); println!("{}", lca.unwrap()); }
Attempts:
2 left
💡 Hint
Both nodes 7 and 4 are children of node 2.
✗ Incorrect
The lowest common ancestor of nodes 7 and 4 is node 2 because both are direct children of node 2.
🔧 Debug
advancedIdentify the error in LCA function implementation
Which option describes the error in this Rust LCA function implementation that causes it to fail when both nodes are on the same subtree?
DSA Rust
fn lowest_common_ancestor(root: Option<Rc<RefCell<TreeNode>>>, p: i32, q: i32) -> Option<i32> {
fn helper(node: &Option<Rc<RefCell<TreeNode>>>, p: i32, q: i32) -> Option<Rc<RefCell<TreeNode>>> {
if let Some(n) = node {
let n_borrow = n.borrow();
if n_borrow.val == p || n_borrow.val == q {
return Some(Rc::clone(n));
}
let left = helper(&n_borrow.left, p, q);
let right = helper(&n_borrow.right, p, q);
if left.is_some() && right.is_some() {
return Some(Rc::clone(n));
}
if left.is_some() {
return left;
}
if right.is_some() {
return right;
}
None
} else {
None
}
}
helper(&root, p, q).map(|node| node.borrow().val)
}Attempts:
2 left
💡 Hint
Check the return logic when only one subtree returns a node.
✗ Incorrect
When both nodes are in the same subtree, the function should return that subtree's result, but if the logic is incorrect, it might return None.
🧠 Conceptual
advancedUnderstanding LCA properties in binary trees
Which statement about the Lowest Common Ancestor (LCA) in a binary tree is true?
Attempts:
2 left
💡 Hint
Think about the definition of ancestor and descendant including the node itself.
✗ Incorrect
By definition, the LCA is the deepest node that has both nodes as descendants, and a node can be a descendant of itself.
🚀 Application
expertNumber of nodes in subtree rooted at LCA
Given a binary tree and two nodes p and q, after finding their Lowest Common Ancestor (LCA), how many nodes are in the subtree rooted at the LCA if the subtree includes the LCA node itself?
DSA Rust
/* Assume the same tree as previous examples */ // Function to count nodes in subtree fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { if let Some(node) = root { let n = node.borrow(); 1 + count_nodes(n.left.clone()) + count_nodes(n.right.clone()) } else { 0 } } fn main() { // Tree setup as before let node5 = Rc::new(RefCell::new(TreeNode { val: 5, left: None, right: None })); let node1 = Rc::new(RefCell::new(TreeNode { val: 1, left: None, right: None })); let node6 = Rc::new(RefCell::new(TreeNode { val: 6, left: None, right: None })); let node2 = Rc::new(RefCell::new(TreeNode { val: 2, left: None, right: None })); let node0 = Rc::new(RefCell::new(TreeNode { val: 0, left: None, right: None })); let node8 = Rc::new(RefCell::new(TreeNode { val: 8, left: None, right: None })); let node7 = Rc::new(RefCell::new(TreeNode { val: 7, left: None, right: None })); let node4 = Rc::new(RefCell::new(TreeNode { val: 4, left: None, right: None })); node5.borrow_mut().left = Some(Rc::clone(&node6)); node5.borrow_mut().right = Some(Rc::clone(&node2)); node2.borrow_mut().left = Some(Rc::clone(&node7)); node2.borrow_mut().right = Some(Rc::clone(&node4)); node1.borrow_mut().left = Some(Rc::clone(&node0)); node1.borrow_mut().right = Some(Rc::clone(&node8)); let root = Some(Rc::clone(&node5)); let lca_node_val = lowest_common_ancestor(root.clone(), 7, 4).unwrap(); // Find the LCA node reference fn find_node(root: Option<Rc<RefCell<TreeNode>>>, val: i32) -> Option<Rc<RefCell<TreeNode>>> { if let Some(node) = root { if node.borrow().val == val { return Some(Rc::clone(&node)); } let left = find_node(node.borrow().left.clone(), val); if left.is_some() { return left; } find_node(node.borrow().right.clone(), val) } else { None } } let lca_node = find_node(root, lca_node_val); let count = count_nodes(lca_node); println!("{}", count); }
Attempts:
2 left
💡 Hint
Count all nodes under node 2 including itself.
✗ Incorrect
The subtree rooted at node 2 includes nodes 2, 7, and 4. Nodes 7 and 4 have no children, so total nodes are 3.
