Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new binary tree node with given value.
DSA Rust
fn new_node(val: i32) -> Box<TreeNode> {
Box::new(TreeNode {
val: [1],
left: None,
right: None,
})
}Attempts:
3 left
💡 Hint
Common Mistakes
Using None instead of val for the node's value.
Assigning 0 instead of the given val.
✗ Incorrect
The function should assign the passed value 'val' to the node's val field.
2fill in blank
mediumComplete the code to insert a node into the binary tree at the correct horizontal distance.
DSA Rust
fn insert_node(
node: &mut Option<Box<TreeNode>>,
val: i32,
hd: i32,
map: &mut std::collections::HashMap<i32, i32>
) {
if let Some(n) = node {
if !map.contains_key(&hd) {
map.insert(hd, [1]);
}
insert_node(&mut n.left, val, hd - 1, map);
insert_node(&mut n.right, val, hd + 1, map);
} else {
*node = Some(Box::new(TreeNode { val, left: None, right: None }));
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Inserting the horizontal distance instead of the node's value.
Trying to insert map values instead of the node's value.
✗ Incorrect
We insert the node's value at the horizontal distance if it is not already present.
3fill in blank
hardFix the error in the code to correctly collect the top view nodes from the map.
DSA Rust
fn top_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
let mut map = std::collections::BTreeMap::new();
fn traverse(node: &Option<Box<TreeNode>>, hd: i32, map: &mut std::collections::BTreeMap<i32, i32>) {
if let Some(n) = node {
map.entry(hd).[1];
traverse(&n.left, hd - 1, map);
traverse(&n.right, hd + 1, map);
}
}
traverse(&root, 0, &mut map);
map.values().cloned().collect::<Vec<i32>>()
}Attempts:
3 left
💡 Hint
Common Mistakes
Using or_insert directly with n.val causing borrow checker errors.
Using insert which overwrites existing entries.
✗ Incorrect
or_insert_with with a closure ensures n.val is evaluated only if needed, avoiding borrowing issues.
4fill in blank
hardFill both blanks to correctly implement the top view traversal and map insertion.
DSA Rust
fn traverse(node: &Option<Box<TreeNode>>, hd: i32, map: &mut std::collections::BTreeMap<i32, i32>) {
if let Some(n) = node {
map.entry(hd).[1](|| n.val);
traverse(&n.[2], hd - 1, map);
traverse(&n.right, hd + 1, map);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using or_insert instead of or_insert_with causing borrow issues.
Using n.right instead of n.left for left subtree traversal.
✗ Incorrect
Use or_insert_with to insert lazily, and traverse left child with n.left.
5fill in blank
hardFill all three blanks to complete the function that returns the top view of the binary tree.
DSA Rust
fn top_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
let mut map = std::collections::BTreeMap::new();
fn traverse(node: &Option<Box<TreeNode>>, hd: i32, map: &mut std::collections::BTreeMap<i32, i32>) {
if let Some(n) = node {
map.entry(hd).[1](|| n.val);
traverse(&n.[2], hd - 1, map);
traverse(&n.[3], hd + 1, map);
}
}
traverse(&root, 0, &mut map);
map.values().cloned().collect()
}Attempts:
3 left
💡 Hint
Common Mistakes
Using or_insert instead of or_insert_with.
Mixing up left and right child traversal.
✗ Incorrect
Use or_insert_with for lazy insertion, and traverse left and right children correctly.
