0
0
DSA Javascriptprogramming~10 mins

Mirror a Binary Tree in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Mirror a Binary Tree
Start at root node
Swap left and right children
Mirror left subtree
Mirror right subtree
Return mirrored node
Done
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
StepOperationNode VisitedPointer ChangesTree State
1Start at root1None1 / \ 2 3
2Swap children1left=3, right=21 / \ 3 2
3Mirror left subtree3None1 / \ 3 2
4Swap children3left=6, right=51 / \ 3 2 / \ 6 5
5Mirror left subtree6None1 / \ 3 2 / \ 6 5
6Swap children6left=null, right=null1 / \ 3 2 / \ 6 5
7Mirror left subtreenullNoneNo change
8Mirror right subtreenullNoneNo change
9Mirror right subtree5None1 / \ 3 2 / \ 6 5
10Swap children5left=null, right=null1 / \ 3 2 / \ 6 5
11Mirror left subtreenullNoneNo change
12Mirror right subtreenullNoneNo change
13Mirror right subtree2None1 / \ 3 2
14Swap children2left=4, right=null1 / \ 3 2 / 4 null
15Mirror left subtree4None1 / \ 3 2 / 4 null
16Swap children4left=null, right=null1 / \ 3 2 / 4 null
17Mirror left subtreenullNoneNo change
18Mirror right subtreenullNoneNo change
19Mirror right subtreenullNoneNo change
20Return mirrored root1None1 / \ 3 2 / \ / 6 5 4 null
21DoneNoneNoneFinal mirrored tree
💡 All nodes visited and children swapped, recursion ends.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 10After Step 14Final
noderoot(1)root(1)node(3)node(6)node(5)node(2)root(1)
node.left236nullnull43
node.right325nullnullnull2
Tree State1 / \ 2 31 / \ 3 21 / \ 3 2 / \ 6 5No changeNo change1 / \ 3 2 / 4 null1 / \ 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.