Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a binary tree node struct with optional children.
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
impl TreeNode {
fn new(val: i32) -> Self {
TreeNode {
val,
left: [1],
right: None,
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Some(...) instead of None for children in new node.
Trying to assign a raw TreeNode instead of Boxed TreeNode.
✗ Incorrect
When creating a new node, its children should start as None (no children).
2fill in blank
mediumComplete the function signature to check if a binary tree is balanced.
DSA Rust
fn is_balanced(root: &Option<Box<TreeNode>>) -> bool {
[1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true unconditionally.
Checking only if root is_some without balance logic.
✗ Incorrect
The tree is balanced if empty or if check_balance returns true in its first value.
3fill in blank
hardFix the error in the helper function that returns (bool, i32) for balance and height.
DSA Rust
fn check_balance(node: &Option<Box<TreeNode>>) -> (bool, i32) {
if node.is_none() {
return (true, 0);
}
let node_ref = node.as_ref().unwrap();
let (left_balanced, left_height) = check_balance(&node_ref.left);
let (right_balanced, right_height) = check_balance(&node_ref.right);
let balanced = left_balanced && right_balanced && (left_height - right_height).abs() [1] 1;
let height = 1 + left_height.max(right_height);
(balanced, height)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using == 1 instead of <= 1 causes imbalance detection errors.
Using > or >= reverses the logic.
✗ Incorrect
The tree is balanced if the height difference is at most 1 (<= 1).
4fill in blank
hardFill both blanks to complete the recursive height calculation and balance check.
DSA Rust
fn check_balance(node: &Option<Box<TreeNode>>) -> (bool, i32) {
if node.is_none() {
return (true, 0);
}
let node_ref = node.as_ref().unwrap();
let (left_balanced, left_height) = check_balance(&node_ref.[1]);
let (right_balanced, right_height) = check_balance(&node_ref.[2]);
let balanced = left_balanced && right_balanced && (left_height - right_height).abs() <= 1;
let height = 1 + left_height.max(right_height);
(balanced, height)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using val or new instead of left or right causes errors.
Swapping left and right changes logic but still compiles.
✗ Incorrect
We recursively check the left and right children for balance and height.
5fill in blank
hardFill all three blanks to complete the main function calling the helper and returning the balance result.
DSA Rust
fn is_balanced(root: &Option<Box<TreeNode>>) -> bool {
if root.[1]() {
return true;
}
let (balanced, _) = check_balance(&root.[2]);
balanced && check_balance(&root.[3]).0
}Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap() instead of is_none() causes panic on empty tree.
Checking only one child subtree misses full balance check.
✗ Incorrect
Check if root is None, then check balance of left and right subtrees.
