Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the total number of nodes in the binary tree.
DSA Rust
fn count_nodes(root: &Option<Box<TreeNode>>) -> i32 {
match root {
None => 0,
Some(node) => 1 + count_nodes(&node.left) + count_nodes(&node[1])
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '.left' again instead of '.right' causes counting only left subtree twice.
Using '.value' or '.parent' is incorrect because they are not child nodes.
✗ Incorrect
We count nodes by adding 1 for the current node plus counts of left and right subtrees. The blank should be '.right' to access the right child.
2fill in blank
mediumComplete the code to create a new TreeNode with given value.
DSA Rust
fn new_node(val: i32) -> Box<TreeNode> {
Box::new(TreeNode {
value: val,
left: None,
[1]: None,
})
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'child' which are not fields in the TreeNode struct.
Using 'node' which is not a field name.
✗ Incorrect
A TreeNode has 'left' and 'right' children. To initialize the right child as empty, use 'right: None'.
3fill in blank
hardFix the error in the recursive call to count nodes in the right subtree.
DSA Rust
fn count_nodes(root: &Option<Box<TreeNode>>) -> i32 {
match root {
None => 0,
Some(node) => 1 + count_nodes(&node.left) + count_nodes(&node[1])
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Typo in field name like '.leftt' causes compile error.
Using '.value' instead of child node fields.
✗ Incorrect
The correct field name for the right child is '.right'. '.leftt' is a typo and '.value' is not a child node.
4fill in blank
hardFill both blanks to create a function that counts nodes only if the node's value is greater than 0.
DSA Rust
fn count_positive_nodes(root: &Option<Box<TreeNode>>) -> i32 {
match root {
None => 0,
Some(node) => {
let count_left = count_positive_nodes(&node[1]);
let count_right = count_positive_nodes(&node[2]);
if node.value > 0 { 1 + count_left + count_right } else { count_left + count_right }
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '.parent' or '.value' which are not child nodes.
Swapping left and right fields.
✗ Incorrect
To count nodes in left and right subtrees, use '.left' and '.right' respectively.
5fill in blank
hardFill all three blanks to create a function that counts nodes with values less than a given threshold.
DSA Rust
fn count_nodes_less_than(root: &Option<Box<TreeNode>>, threshold: i32) -> i32 {
match root {
None => 0,
Some(node) => {
let left_count = count_nodes_less_than(&node[1], threshold);
let right_count = count_nodes_less_than(&node[2], threshold);
if node.value [3] threshold { 1 + left_count + right_count } else { left_count + right_count }
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' changes the condition logic.
Using '.parent' or '.value' instead of child nodes.
✗ Incorrect
We recurse on left and right children using '.left' and '.right'. To check if node value is less than threshold, use '<'.
