Challenge - 5 Problems
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code for path sum check?
Given the binary tree and target sum, what does the function return?
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 has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool { match root { None => false, Some(node) => { let node = node.borrow(); if node.left.is_none() && node.right.is_none() { return node.val == target_sum; } has_path_sum(node.left.clone(), target_sum - node.val) || has_path_sum(node.right.clone(), target_sum - node.val) } } } fn main() { let root = TreeNode::new(5); root.borrow_mut().left = Some(TreeNode::new(4)); root.borrow_mut().right = Some(TreeNode::new(8)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(11)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(13)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(4)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(7)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(2)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().right.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(1)); let result = has_path_sum(Some(root), 22); println!("{}", result); }
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums exactly to 22.
✗ Incorrect
The path 5->4->11->2 sums to 22, so the function returns true.
❓ Predict Output
intermediateWhat is the output of this Rust code for path sum check with no valid path?
Given the binary tree and target sum, what does the function return?
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 has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool { match root { None => false, Some(node) => { let node = node.borrow(); if node.left.is_none() && node.right.is_none() { return node.val == target_sum; } has_path_sum(node.left.clone(), target_sum - node.val) || has_path_sum(node.right.clone(), target_sum - node.val) } } } fn main() { let root = TreeNode::new(1); root.borrow_mut().left = Some(TreeNode::new(2)); root.borrow_mut().right = Some(TreeNode::new(3)); let result = has_path_sum(Some(root), 5); println!("{}", result); }
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums exactly to 5.
✗ Incorrect
No root-to-leaf path sums to 5 (paths are 1->2=3 and 1->3=4).
🔧 Debug
advancedWhat error does this Rust code produce when checking path sum?
Identify the error when running this code snippet.
DSA Rust
fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool { if root.is_none() { return false; } let node = root.unwrap().borrow(); if node.left.is_none() && node.right.is_none() { return node.val == target_sum; } has_path_sum(node.left.clone(), target_sum - node.val) || has_path_sum(node.right.clone(), target_sum - node.val) }
Attempts:
2 left
💡 Hint
Check how unwrap is used on borrowed data.
✗ Incorrect
Calling unwrap() on root moves the value, but root is borrowed, causing a move error.
🧠 Conceptual
advancedHow does the path sum function handle leaf nodes in a binary tree?
In the path sum algorithm, what condition identifies a leaf node and what check is performed?
Attempts:
2 left
💡 Hint
Leaf nodes have no left or right children.
✗ Incorrect
Leaf nodes are identified by no left and right children; the function checks if node value equals the target sum at that point.
🚀 Application
expertWhat is the number of root-to-leaf paths with sum 15 in this tree?
Given the tree below, how many root-to-leaf paths sum to 15?
Tree structure:
- Root 5
- Left 4
- Left 11
- Left 7
- Right 2
- Right 8
- Left 13
- Right 4
- Right 1
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_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> i32 { match root { None => 0, Some(node) => { let node = node.borrow(); if node.left.is_none() && node.right.is_none() { return if node.val == target_sum { 1 } else { 0 }; } count_path_sum(node.left.clone(), target_sum - node.val) + count_path_sum(node.right.clone(), target_sum - node.val) } } } fn main() { let root = TreeNode::new(5); root.borrow_mut().left = Some(TreeNode::new(4)); root.borrow_mut().right = Some(TreeNode::new(8)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(11)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(13)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(4)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(7)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(2)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().right.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(1)); let count = count_path_sum(Some(root), 15); println!("{}", count); }
Attempts:
2 left
💡 Hint
Count all root-to-leaf paths where sum equals 15.
✗ Incorrect
No root-to-leaf path sums to 15 in the given tree. The paths and their sums are:
- 5->4->11->7 = 27
- 5->4->11->2 = 22
- 5->8->13 = 26
- 5->8->4->1 = 18
Thus, the count is 0.
