Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the left and right child nodes of the root.
DSA Rust
fn mirror(root: &mut Option<Box<TreeNode>>) {
if let Some(node) = root.as_mut() {
let temp = node.left.take();
node.left = node.right.take();
node.right = [1];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning node.left to node.right without using a temporary variable causes loss of data.
Using None instead of temp loses the original left subtree.
✗ Incorrect
We store the original left child in temp, then assign the right child to left, and finally assign temp to right to complete the swap.
2fill in blank
mediumComplete the code to recursively mirror the left subtree.
DSA Rust
fn mirror(root: &mut Option<Box<TreeNode>>) {
if let Some(node) = root.as_mut() {
mirror(&mut node.left);
mirror(&mut node.right);
let temp = node.left.take();
node.left = node.right.take();
node.right = temp;
}
}
// Call mirror on the root
mirror(&mut [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Passing node instead of root causes errors because node is not in scope here.
Passing None does nothing.
✗ Incorrect
The function mirror is called on the root node to start the mirroring process from the top.
3fill in blank
hardFix the error in the code to correctly mirror the binary tree.
DSA Rust
fn mirror(root: &mut Option<Box<TreeNode>>) {
if root.is_none() {
return;
}
let node = root.as_mut().unwrap();
mirror(&mut node.left);
mirror(&mut node.right);
let temp = node.left.take();
node.left = node.right.take();
node.right = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning node.right to node.left causes incorrect swapping.
Assigning None loses the subtree.
✗ Incorrect
The variable temp holds the original left subtree and must be assigned to node.right to complete the swap.
4fill in blank
hardFill both blanks to create a function that mirrors a binary tree recursively.
DSA Rust
fn mirror(root: &mut Option<Box<TreeNode>>) {
if [1] {
return;
}
let node = root.as_mut().unwrap();
mirror(&mut node.left);
mirror(&mut node.right);
let temp = node.left.take();
node.left = node.right.take();
node.right = [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using root.is_some() in the if condition causes the function to return too early.
Assigning node.right to node.left instead of temp causes incorrect swapping.
✗ Incorrect
The function returns immediately if the root is None (empty tree). Then it swaps left and right children using temp.
5fill in blank
hardFill all three blanks to implement a complete mirror function for a binary tree.
DSA Rust
fn mirror(root: &mut Option<Box<TreeNode>>) {
if [1] {
return;
}
let node = root.as_mut().unwrap();
mirror(&mut [2]);
mirror(&mut [3]);
let temp = node.left.take();
node.left = node.right.take();
node.right = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root instead of node's children causes infinite recursion.
Swapping children before recursive calls leads to incorrect results.
✗ Incorrect
The function returns if the tree is empty. Then it recursively mirrors left and right subtrees before swapping them.
