The process starts at the root, swaps its children, then recursively mirrors left and right subtrees until all nodes are processed.
Execution Sample
DSA Javascript
function mirror(node) {
if (!node) return null;
[node.left, node.right] = [node.right, node.left];
mirror(node.left);
mirror(node.right);
return node;
}
This function swaps left and right children of each node recursively to create the mirror image of the binary tree.
Execution Table
Step
Operation
Node Visited
Pointer Changes
Tree State
1
Start at root
1
None
1
/ \
2 3
2
Swap children
1
left=3, right=2
1
/ \
3 2
3
Mirror left subtree
3
None
1
/ \
3 2
4
Swap children
3
left=6, right=5
1
/ \
3 2
/ \
6 5
5
Mirror left subtree
6
None
1
/ \
3 2
/ \
6 5
6
Swap children
6
left=null, right=null
1
/ \
3 2
/ \
6 5
7
Mirror left subtree
null
None
No change
8
Mirror right subtree
null
None
No change
9
Mirror right subtree
5
None
1
/ \
3 2
/ \
6 5
10
Swap children
5
left=null, right=null
1
/ \
3 2
/ \
6 5
11
Mirror left subtree
null
None
No change
12
Mirror right subtree
null
None
No change
13
Mirror right subtree
2
None
1
/ \
3 2
14
Swap children
2
left=4, right=null
1
/ \
3 2
/
4 null
15
Mirror left subtree
4
None
1
/ \
3 2
/
4 null
16
Swap children
4
left=null, right=null
1
/ \
3 2
/
4 null
17
Mirror left subtree
null
None
No change
18
Mirror right subtree
null
None
No change
19
Mirror right subtree
null
None
No change
20
Return mirrored root
1
None
1
/ \
3 2
/ \ /
6 5 4 null
21
Done
None
None
Final mirrored tree
💡 All nodes visited and children swapped, recursion ends.
Variable Tracker
Variable
Start
After Step 2
After Step 4
After Step 6
After Step 10
After Step 14
Final
node
root(1)
root(1)
node(3)
node(6)
node(5)
node(2)
root(1)
node.left
2
3
6
null
null
4
3
node.right
3
2
5
null
null
null
2
Tree State
1
/ \
2 3
1
/ \
3 2
1
/ \
3 2
/ \
6 5
No change
No change
1
/ \
3 2
/
4 null
1
/ \
3 2
/ \ /
6 5 4 null
Key Moments - 3 Insights
Why do we swap children before recursive calls?
Swapping children first (see Step 2) ensures that when recursion goes to node.left and node.right, it processes the mirrored positions, so the entire subtree gets mirrored correctly.
What happens when a node is null?
When node is null (Steps 7, 8, 11, 12, 17, 18, 19), the function returns immediately without changes, stopping recursion on that path.
Why does the tree state update after swapping children?
Because swapping changes the structure visually (Step 2, 4, 10, 14), the tree state reflects the new left and right children positions after each swap.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what are the left and right children of node 1 after Step 2?
Aleft=5, right=6
Bleft=2, right=3
Cleft=3, right=2
Dleft=null, right=null
💡 Hint
Check the 'Pointer Changes' column at Step 2 in the execution table.
At which step does the node 2 swap its children?
AStep 10
BStep 14
CStep 4
DStep 6
💡 Hint
Look for node 2 in the 'Node Visited' column and find when 'Swap children' happens.
If we skip swapping children before recursion, how would the tree state after Step 2 change?
AChildren of node 1 remain as left=2, right=3
BChildren of node 1 become left=3, right=2
CTree becomes empty
DChildren of node 1 become null
💡 Hint
Refer to the 'Pointer Changes' column at Step 2 and understand the effect of swapping.
Concept Snapshot
Mirror a Binary Tree:
- Swap left and right children of each node
- Recursively mirror left subtree
- Recursively mirror right subtree
- Base case: if node is null, return
- Result: tree is flipped horizontally
Full Transcript
To mirror a binary tree, start at the root node. Swap its left and right children. Then recursively apply the same process to the left and right subtrees. If a node is null, return immediately. This process flips the tree horizontally, creating a mirror image. The execution table shows each step visiting nodes, swapping children, and updating the tree structure until all nodes are processed.