Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the TreeNode struct with optional left and right children.
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>, // left child
right: Option<Box<TreeNode>>, // right child
}
impl TreeNode {
fn new(val: i32) -> Self {
TreeNode {
val,
left: [1],
right: None,
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Some without Box causes type errors.
Initializing children with values instead of None.
✗ Incorrect
The left child should be initialized as None when creating a new node without children.
2fill in blank
mediumComplete the code to update the maximum path sum during recursion.
DSA Rust
fn max_gain(node: &Option<Box<TreeNode>>, max_sum: &mut i32) -> i32 {
if let Some(n) = node {
let left_gain = max_gain(&n.left, max_sum).max(0);
let right_gain = max_gain(&n.right, max_sum).max(0);
let price_newpath = n.val + left_gain + right_gain;
*max_sum = (*max_sum).[1](price_newpath);
n.val + left_gain.max(right_gain)
} else {
0
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max causes wrong results.
Forgetting to update max_sum.
✗ Incorrect
We update max_sum to the maximum of current max_sum and the new path sum.
3fill in blank
hardFix the error in the recursive function to correctly handle the base case.
DSA Rust
fn max_gain(node: &Option<Box<TreeNode>>, max_sum: &mut i32) -> i32 {
if node.is_none() {
return [1];
}
let n = node.as_ref().unwrap();
let left_gain = max_gain(&n.left, max_sum).max(0);
let right_gain = max_gain(&n.right, max_sum).max(0);
let price_newpath = n.val + left_gain + right_gain;
*max_sum = (*max_sum).max(price_newpath);
n.val + left_gain.max(right_gain)
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning n.val causes panic on None.
Returning negative values reduces max path sum incorrectly.
✗ Incorrect
The base case returns 0 because a null node contributes no gain.
4fill in blank
hardFill both blanks to create a function that initializes max_sum and calls the recursive helper.
DSA Rust
pub fn max_path_sum(root: Option<Box<TreeNode>>) -> i32 {
let mut max_sum = [1];
max_gain(&root, &mut max_sum);
[2]
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing max_sum to 0 causes errors with negative values.
Not returning max_sum causes function to return unit type.
✗ Incorrect
max_sum starts at the smallest integer to allow any path sum to update it. The function returns max_sum after recursion.
5fill in blank
hardFill all three blanks to complete the recursive max_gain function with correct logic.
DSA Rust
fn max_gain(node: &Option<Box<TreeNode>>, max_sum: &mut i32) -> i32 {
if node.is_none() {
return 0;
}
let n = node.as_ref().unwrap();
let left_gain = max_gain(&n.left, max_sum).[1](0);
let right_gain = max_gain(&n.right, max_sum).[2](0);
let price_newpath = n.val + left_gain + right_gain;
*max_sum = (*max_sum).max(price_newpath);
n.val + left_gain.[3](right_gain)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max causes wrong path sums.
Not ignoring negative gains leads to smaller sums.
✗ Incorrect
We use max(0) to ignore negative gains, and max to choose the larger gain to continue the path.
