Concept Flow - Mirror a Binary Tree
Start at root node
Swap left and right child
Mirror left subtree
Mirror right subtree
Return mirrored subtree
Start from the root, swap its left and right children, then recursively do the same for each subtree.
fn mirror(node: &mut Option<Box<TreeNode>>) {
if let Some(n) = node {
let left = n.left.take();
let right = n.right.take();
n.left = right;
n.right = left;
mirror(&mut n.left);
mirror(&mut n.right);
}
}| Step | Operation | Node Visited | Pointer Changes | Tree State (Visual) |
|---|---|---|---|---|
| 1 | Start at root | 1 | None | 1 / \ 2 3 / \ \ 4 5 6 |
| 2 | Swap children of node 1 | 1 | left=3, right=2 | 1 / \ 3 2 \ / \ 6 4 5 |
| 3 | Mirror left subtree of node 1 (node 3) | 3 | None | 1 / \ 3 2 \ / \ 6 4 5 |
| 4 | Swap children of node 3 | 3 | left=None, right=6 | 1 / \ 3 2 / / \ None 4 5 \ 6 |
| 5 | Mirror left subtree of node 3 (None) | None | None | No change |
| 6 | Mirror right subtree of node 3 (node 6) | 6 | None | 1 / \ 3 2 / / \ None 4 5 \ 6 |
| 7 | Swap children of node 6 | 6 | left=None, right=None | 1 / \ 3 2 / / \ None 4 5 \ 6 |
| 8 | Mirror left subtree of node 6 (None) | None | None | No change |
| 9 | Mirror right subtree of node 6 (None) | None | None | No change |
| 10 | Mirror right subtree of node 1 (node 2) | 2 | None | 1 / \ 3 2 / / \ None 4 5 \ 6 |
| 11 | Swap children of node 2 | 2 | left=5, right=4 | 1 / \ 3 2 / / \ None 5 4 \ 6 |
| 12 | Mirror left subtree of node 2 (node 5) | 5 | None | 1 / \ 3 2 / / \ None 5 4 \ 6 |
| 13 | Swap children of node 5 | 5 | left=None, right=None | No change |
| 14 | Mirror left subtree of node 5 (None) | None | None | No change |
| 15 | Mirror right subtree of node 5 (None) | None | None | No change |
| 16 | Mirror right subtree of node 2 (node 4) | 4 | None | 1 / \ 3 2 / / \ None 5 4 \ 6 |
| 17 | Swap children of node 4 | 4 | left=None, right=None | No change |
| 18 | Mirror left subtree of node 4 (None) | None | None | No change |
| 19 | Mirror right subtree of node 4 (None) | None | None | No change |
| 20 | Return from root | 1 | None | 1 / \ 3 2 / / \ None 5 4 \ 6 |
| Variable | Start | After Step 2 | After Step 4 | After Step 11 | Final |
|---|---|---|---|---|---|
| node | root=1 | root=1 | node=3 | node=2 | root=1 |
| left child | 2 | 3 | None | 5 | 3 or 5 depending on subtree |
| right child | 3 | 2 | 6 | 4 | 2 or 4 depending on subtree |
| tree state | Original tree | Root children swapped | Node 3 children swapped | Node 2 children swapped | Fully mirrored tree |
Mirror a Binary Tree: - Swap left and right children of each node - Recursively mirror left and right subtrees - Base case: node is None (stop) - Use recursion to traverse entire tree - Result: tree structure flipped horizontally
take() to avoid borrowing issues.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.take() to swap safely [OK]take() to swap children safely in Rust [OK]take() causing borrow errorsstruct 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?fn mirror(node: &mut Option) {
if let Some(n) = node {
mirror(&mut n.left);
mirror(&mut n.right);
n.left = n.right.take();
n.right = n.left.take();
}
}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.Pass the test and this pattern moves unknown