Mirroring a binary tree means swapping the left and right children of every node in the tree, creating a mirror image of the original tree.
Click to reveal answer
beginner
In Rust, which data structure is commonly used to represent a binary tree node?
A binary tree node is often represented using an enum with variants for None and Some(Box<TreeNode>), where TreeNode is a struct containing the value and optional left and right children.
Click to reveal answer
beginner
What is the base case when recursively mirroring a binary tree?
The base case is when the current node is None (empty). In this case, the function returns immediately without changes.
Click to reveal answer
intermediate
How do you swap the left and right children of a node in Rust safely?
You can use the std::mem::swap function to exchange the left and right children references safely without losing ownership.
Click to reveal answer
intermediate
Why is recursion a natural fit for mirroring a binary tree?
Because each node's mirror depends on mirroring its left and right subtrees, recursion allows the function to apply the same logic to smaller parts until reaching the base case.
Click to reveal answer
What happens to the left child of a node when mirroring a binary tree?
AIt becomes the right child.
BIt stays the left child.
CIt is deleted.
DIt becomes the parent.
✗ Incorrect
Mirroring swaps the left and right children, so the left child becomes the right child.
In Rust, what type is commonly used to represent a possibly empty child node in a binary tree?
AResult<TreeNode, Error>
BOption<Box<TreeNode>>
CTreeNode
DVec<TreeNode>
✗ Incorrect
Option> is used to represent a child that may or may not exist.
Which Rust function helps swap two mutable references safely?
Astd::mem::forget
Bstd::mem::replace
Cstd::mem::drop
Dstd::mem::swap
✗ Incorrect
std::mem::swap exchanges the values of two mutable references safely.
What is the time complexity of mirroring a binary tree with n nodes?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Each node is visited once, so the time complexity is linear, O(n).
What is the first step in the recursive mirror function for a node?
ACreate a new node.
BSwap the node's value.
CCheck if the node is None.
DPrint the node's value.
✗ Incorrect
The function first checks if the node is None to stop recursion.
Explain how to mirror a binary tree using recursion in Rust.
Think about visiting each node and swapping its children after recursive calls.
You got /4 concepts.
Describe the structure of a binary tree node in Rust and how it supports mirroring.
Focus on how Rust handles ownership and optional children.
You got /4 concepts.
Practice
(1/5)
1. What does it mean to mirror a binary tree?
easy
A. Swap the left and right children of every node
B. Remove all leaf nodes
C. Convert the tree into a linked list
D. Sort the nodes in ascending order
Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node in the tree.
Step 2: Effect on the tree structure
This operation creates a tree that is a mirror image of the original, flipping it horizontally.
Final Answer:
Swap the left and right children of every node -> Option A
Quick Check:
Mirror = Swap children [OK]
Hint: Mirror means swapping left and right children at all nodes [OK]
Common Mistakes:
Thinking mirror means deleting nodes
Confusing mirror with sorting nodes
Assuming mirror changes node values
2. Which Rust code snippet correctly swaps the left and right children of a binary tree node?
easy
A. let temp = node.left.take(); node.left = node.right.take(); node.right = temp;
B. node.left = node.left; node.right = node.right;
C. node.left = node.right; node.right = node.left;
D. node.left = None; node.right = None;
Solution
Step 1: Understand Rust ownership for swapping
In Rust, to swap children, we must take ownership using take() to avoid borrowing issues.
Step 2: Analyze the code snippets
let temp = node.left.take(); node.left = node.right.take(); node.right = temp; uses take() to temporarily hold left child, then swaps left and right correctly. node.left = node.right; node.right = node.left; incorrectly assigns right to left and then left to right without temp, causing both to be same.
Final Answer:
let temp = node.left.take(); node.left = node.right.take(); node.right = temp; -> Option A
Quick Check:
Use take() to swap safely [OK]
Hint: Use take() to swap children safely in Rust [OK]
Common Mistakes:
Assigning children without temporary variable
Not using take() causing borrow errors
Setting children to None accidentally
3. Given the Rust code below that mirrors a binary tree, what is the output when printed in-order?
struct Node {
val: i32,
left: Option,
right: Option,
}
fn mirror(node: &mut Option) {
if let Some(n) = node {
mirror(&mut n.left);
mirror(&mut n.right);
let temp = n.left.take();
n.left = n.right.take();
n.right = temp;
}
}
// Tree before mirror:
// 1
// / \
// 2 3
// / \
// 4 5
// In-order traversal after mirror?
medium
A. [3, 5, 1, 2, 4]
B. [4, 2, 1, 3, 5]
C. [1, 2, 3, 4, 5]
D. [5, 3, 1, 4, 2]
Solution
Step 1: Understand the original tree and mirror effect
Original tree in-order: left, root, right = [4, 2, 1, 3, 5]. Mirror swaps left and right children at every node.
A. Using immutable borrow instead of mutable borrow
B. Missing base case for empty node
C. Swapping children incorrectly causes both to be None
D. Recursion order is wrong
Solution
Step 1: Analyze the swapping lines
The code assigns n.left = n.right.take() first, which moves right child to left and sets right to None. Then n.right = n.left.take() tries to take from left, which is now the old right child, but after first take, left is already moved, so it becomes None.
Step 2: Identify the bug
Both children end up None because the second take is from a moved value. Correct approach is to use a temporary variable to hold one child before swapping.
Final Answer:
Swapping children incorrectly causes both to be None -> Option C
Quick Check:
Swap needs temp variable to avoid losing nodes [OK]
Hint: Use temp variable to swap children safely [OK]
Common Mistakes:
Swapping without temp causes data loss
Ignoring ownership rules in Rust
Assuming recursion order fixes swap
5. You want to mirror a binary tree and then print its level order traversal. Which approach correctly combines mirroring and level order traversal in Rust?
hard
A. Print level order first, then mirror the tree recursively
B. First mirror the tree recursively, then use a queue to print nodes level by level
C. Mirror the tree by swapping values only, then print in-order traversal
D. Use recursion to print level order while mirroring simultaneously
Solution
Step 1: Understand the correct order of operations
Mirroring must be done first to change the tree structure. Then level order traversal prints nodes level by level using a queue.
Step 2: Analyze options
First mirror the tree recursively, then use a queue to print nodes level by level correctly mirrors recursively first, then then uses a queue for level order. Print level order first, then mirror the tree recursively prints before mirror, so output is original. Mirror the tree by swapping values only, then print in-order traversal swaps values only, not structure. Use recursion to print level order while mirroring simultaneously tries recursion for level order which is not straightforward.
Final Answer:
First mirror the tree recursively, then use a queue to print nodes level by level -> Option B
Quick Check:
Mirror first, then level order with queue [OK]
Hint: Mirror tree first, then use queue for level order [OK]
Common Mistakes:
Printing before mirroring
Trying to print level order recursively
Swapping values instead of nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.