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.
Practice
(1/5)
1. What does it mean to mirror a binary tree?
easy
A. Sort the nodes in ascending order
B. Swap the left and right children of every node in the tree
C. Convert the tree into a linked list
D. Remove all leaf nodes from the tree
Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node.
Step 2: Apply the definition to the tree
By swapping children at every node, the tree becomes a mirror image of the original.
Final Answer:
Swap the left and right children of every node in the tree -> Option B
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
Assuming mirror flattens the tree
2. Which of the following is the correct JavaScript syntax to swap the left and right children of a node named node?
easy
A. let temp = node.left; node.left = node.right; node.right = temp;
B. node.left = node.right; node.right = node.left;
C. node.left, node.right = node.right, node.left;
D. swap(node.left, node.right);
Solution
Step 1: Understand swapping variables in JavaScript
To swap two variables, use a temporary variable to hold one value during the swap.
Step 2: Check each option
let temp = node.left; node.left = node.right; node.right = temp; correctly uses a temporary variable to swap node.left and node.right. node.left = node.right; node.right = node.left; overwrites node.left before assigning node.right. node.left, node.right = node.right, node.left; uses invalid syntax in JavaScript. swap(node.left, node.right); calls an undefined function.
Final Answer:
let temp = node.left; node.left = node.right; node.right = temp; -> Option A
Quick Check:
Use temp variable to swap [OK]
Hint: Use a temp variable to swap two values in JavaScript [OK]
Common Mistakes:
Overwriting one child before saving it
Using Python-style tuple swap syntax
Calling undefined swap functions
3. What is the output of the following code that mirrors a binary tree?
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function mirror(node) {
if (!node) return null;
let left = mirror(node.left);
let right = mirror(node.right);
node.left = right;
node.right = left;
return node;
}
// Tree before mirroring:
// 1
// / \
// 2 3
// / \
// 4 5
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.right = new Node(5);
mirror(root);
function printTree(node) {
if (!node) return 'null';
return node.val + ' -> ' + printTree(node.left) + ', ' + printTree(node.right);
}
console.log(printTree(root));
medium
A. 1 -> 2 -> 4 -> null, 3 -> 5 -> null, null
B. 1 -> 3 -> 4 -> null, 2 -> 5 -> null, null
C. 1 -> 3 -> null, 2 -> 5 -> null, 4
D. 1 -> 3 -> 5 -> null, 2 -> 4 -> null, null
Solution
Step 1: Understand the mirror function
The mirror function swaps left and right children recursively for every node.
Step 2: Trace the tree after mirroring
Original tree: 1 with left=2 and right=3; 2 has left=4; 3 has right=5. After mirroring: 1's left becomes 3, right becomes 2. 3's left becomes 5, right becomes null. 2's left becomes null, right becomes 4. Printing in preorder with left then right shows: 1 -> 3 -> 5 -> null, 2 -> 4 -> null, null.
4. Find the error in the following mirror function and select the correct fix:
function mirror(node) {
if (node == null) return;
mirror(node.left);
mirror(node.right);
let temp = node.left;
node.left = node.right;
node.right = temp;
}
medium
A. The function should return the node after swapping children
B. The base case should check for undefined instead of null
C. The swap should happen before recursive calls
D. The temp variable should be declared outside the function
Solution
Step 1: Analyze the function behavior
The function swaps children after recursive calls but does not return the node, so the caller gets undefined.
Step 2: Identify the fix
Returning the node after swapping ensures the mirrored subtree is returned properly for further use.
Final Answer:
The function should return the node after swapping children -> Option A
Quick Check:
Return node after swap [OK]
Hint: Always return the node after modifying it in recursion [OK]
Common Mistakes:
Not returning the node causes undefined results
Swapping before recursion breaks logic
Misunderstanding null vs undefined checks
5. Given a binary tree where some nodes have only one child, how does the mirror function handle these nodes? Consider the tree:
10
/
20
\
30
What will be the structure after mirroring?
hard
A. 10 with left child 30 and right child 20; 20 and 30 have no children
B. 10 with left child 20 and right null; 20 with left null and right 30
C. 10 with left child null and right child 20; 20 with left child 30 and right null
D. 10 with left child null and right child 30; 30 with left child 20 and right null
Solution
Step 1: Understand mirroring nodes with single children
Mirroring swaps left and right children even if one child is null.
Step 2: Apply mirror to given tree
Original: 10 has left=20, right=null 20 has left=null, right=30 After mirror: 10's left becomes null, right becomes 20 20's left becomes 30, right becomes null
Final Answer:
10 with left child null and right child 20; 20 with left child 30 and right null -> Option C
Quick Check:
Mirror swaps children even if one is null [OK]
Hint: Swap children even if one child is missing [OK]
Common Mistakes:
Ignoring null children during swap
Assuming children move to wrong side
Confusing node values with positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.