0
0
DSA Typescriptprogramming~10 mins

Mirror a Binary Tree in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Mirror a Binary Tree
Start at root node
Swap left and right children
Recursively mirror left subtree
Recursively mirror right subtree
Return mirrored subtree
Done when all nodes processed
Start from the root, swap its left and right children, then do the same for each subtree recursively until all nodes are mirrored.
Execution Sample
DSA Typescript
function mirror(node: TreeNode | null): TreeNode | null {
  if (!node) return null;
  [node.left, node.right] = [mirror(node.right), mirror(node.left)];
  return node;
}
This function swaps left and right children of each node recursively to mirror the binary tree.
Execution Table
StepOperationNode VisitedPointer ChangesTree State (Visual)
1Start at root1None 1 / \ 2 3 / \ \ 4 5 6
2Swap children of node 11left <-> right swapped 1 / \ 3 2 / / \ 6 4 5
3Mirror left subtree of node 1 (node 3)3None yet 1 / \ 3 2 / / \ 6 4 5
4Swap children of node 33left <-> right swapped 1 / \ 3 2 \ / \ 6 4 5
5Mirror left subtree of node 3 (null)nullNone 1 / \ 3 2 \ / \ 6 4 5
6Mirror right subtree of node 3 (node 6)6None 1 / \ 3 2 \ / \ 6 4 5
7Swap children of node 66left <-> right swapped (both null) 1 / \ 3 2 \ / \ 6 4 5
8Mirror left subtree of node 6 (null)nullNone 1 / \ 3 2 \ / \ 6 4 5
9Mirror right subtree of node 6 (null)nullNone 1 / \ 3 2 \ / \ 6 4 5
10Mirror right subtree of node 1 (node 2)2None yet 1 / \ 3 2 \ / \ 6 4 5
11Swap children of node 22left <-> right swapped 1 / \ 3 2 \ / \ 6 5 4
12Mirror left subtree of node 2 (node 5)5None 1 / \ 3 2 \ / \ 6 5 4
13Swap children of node 55left <-> right swapped (both null) 1 / \ 3 2 \ / \ 6 5 4
14Mirror left subtree of node 5 (null)nullNone 1 / \ 3 2 \ / \ 6 5 4
15Mirror right subtree of node 5 (null)nullNone 1 / \ 3 2 \ / \ 6 5 4
16Mirror right subtree of node 2 (node 4)4None 1 / \ 3 2 \ / \ 6 5 4
17Swap children of node 44left <-> right swapped (both null) 1 / \ 3 2 \ / \ 6 5 4
18Mirror left subtree of node 4 (null)nullNone 1 / \ 3 2 \ / \ 6 5 4
19Mirror right subtree of node 4 (null)nullNone 1 / \ 3 2 \ / \ 6 5 4
20All nodes processed, return mirrored treenullNone 1 / \ 3 2 \ / \ 6 5 4
💡 All nodes visited and their children swapped, recursion ends.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 11After Step 17Final
node1 (root)13 (left child of root)6 (left child of 3)2 (right child of root)4 (right child of 2)null (end)
left child23nullnull5nullnull
right child326null4nullnull
Key Moments - 3 Insights
Why do we swap children before recursively calling mirror on subtrees?
Swapping first ensures that when recursion goes down, it mirrors the correct new left and right subtrees. See execution_table steps 2 and 4 where swap happens before recursive calls.
What happens when a node is null?
The function returns immediately without changes, as shown in steps 5, 8, 9, 14, 15, 18, 19 where null nodes cause no action.
Why does the tree structure change visually after each swap?
Because swapping left and right pointers changes the physical layout of the tree, as seen in the Tree State column after steps 2, 4, and 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what change happens to node 1?
AIts left and right children are swapped
BIts children are set to null
CIt is deleted
DNo change
💡 Hint
Check the Pointer Changes and Tree State columns at step 2.
At which step does the function process node 6?
AStep 4
BStep 6
CStep 11
DStep 17
💡 Hint
Look for 'Node Visited' column in execution_table.
If we skip swapping children at each node, how would the final tree state differ?
AThe tree would be fully mirrored
BThe tree would be empty
CThe tree would remain unchanged
DOnly leaf nodes would swap
💡 Hint
Refer to the concept_flow and execution_table steps where swapping is key.
Concept Snapshot
Mirror a Binary Tree:
- Swap left and right children of each node
- Do this recursively for all nodes
- Base case: if node is null, return
- Result: tree structure flipped horizontally
- Use recursion to handle all subtrees
Full Transcript
To mirror a binary tree, start at the root node. Swap its left and right children pointers. Then recursively do the same for the left subtree and the right subtree. If a node is null, return immediately. This process flips the tree horizontally. The code swaps children first, then calls mirror recursively on the swapped children. The recursion ends when all nodes have been processed. The tree's visual structure changes after each swap, showing the mirror effect.