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>>,
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 without Box causes type mismatch.
Trying to initialize children with values instead of None.
✗ Incorrect
When creating a new TreeNode, the left and right children are initially empty, so we use None.
2fill in blank
mediumComplete the code to insert a node into the vertical order map with its horizontal distance.
DSA Rust
use std::collections::BTreeMap;
use std::collections::VecDeque;
fn vertical_order(root: Option<Box<TreeNode>>) -> Vec<Vec<i32>> {
let mut map: BTreeMap<i32, Vec<i32>> = BTreeMap::new();
let mut queue: VecDeque<(Box<TreeNode>, i32)> = VecDeque::new();
if let Some(node) = root {
queue.push_back((node, 0));
}
while let Some((node, hd)) = queue.pop_front() {
map.entry(hd).or_insert(Vec::new()).[1](node.val);
if let Some(left) = node.left {
queue.push_back((left, hd - 1));
}
if let Some(right) = node.right {
queue.push_back((right, hd + 1));
}
}
map.values().cloned().collect()
}Attempts:
3 left
💡 Hint
Common Mistakes
Using push_back on Vec causes error because Vec does not have push_back.
Using insert overwrites existing values.
✗ Incorrect
Vec in Rust uses push to add elements at the end.
3fill in blank
hardFix the error in the BFS loop to correctly handle Option when pushing children.
DSA Rust
while let Some((node, hd)) = queue.pop_front() { map.entry(hd).or_insert(Vec::new()).push(node.val); if let Some(left) = node.left [1] { queue.push_back((left, hd - 1)); } if let Some(right) = node.right { queue.push_back((right, hd + 1)); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap moves the node and causes borrow errors.
Using as_ref returns a reference, but queue expects owned Box.
✗ Incorrect
We clone the Box to push it into the queue without moving the original.
4fill in blank
hardFill both blanks to correctly initialize the queue and map for vertical order traversal.
DSA Rust
use std::collections::[1]; fn vertical_order(root: Option<Box<TreeNode>>) -> Vec<Vec<i32>> { let mut map: [2]<i32, Vec<i32>> = [1]::new(); let mut queue: VecDeque<(Box<TreeNode>, i32)> = VecDeque::new(); if let Some(node) = root { queue.push_back((node, 0)); } // BFS loop ... }
Attempts:
3 left
💡 Hint
Common Mistakes
Using HashMap causes unordered output.
Using LinkedList is not suitable for map.
✗ Incorrect
We use BTreeMap to keep keys sorted and initialize it with BTreeMap::new().
5fill in blank
hardFill all three blanks to complete the BFS loop that processes nodes and updates the map and queue.
DSA Rust
while let Some((node, hd)) = queue.pop_front() { map.entry(hd).or_insert(Vec::new()).[1](node.val); if let Some(left) = node.left [2] { queue.push_back((left.[3](), hd - 1)); } if let Some(right) = node.right { queue.push_back((right.clone(), hd + 1)); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using take moves the node and causes errors.
Forgetting to clone causes ownership errors.
✗ Incorrect
We push node.val into the vector, clone left child to push into queue, and call clone() on left.
