0
0
DSA Goprogramming~10 mins

Mirror a Binary Tree in DSA Go - 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 recursively
Mirror right subtree recursively
Return mirrored subtree
Done
Start at the root, swap its left and right children, then recursively do the same for each subtree until all nodes are processed.
Execution Sample
DSA Go
func mirror(node *Node) *Node {
  if node == nil {
    return nil
  }
  node.Left, node.Right = mirror(node.Right), mirror(node.Left)
  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 (Visual)
1Start at root1None 1 / \ 2 3 / \ \ 4 5 6
2Swap children of node 11Left=3, Right=2 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=nil, Right=6 1 / \ 3 2 \ / \ 6 4 5
5Mirror left subtree of node 3 (nil)nilNone 1 / \ 3 2 \ / \ 6 4 5
6Mirror right subtree of node 3 (node 6)6None yet 1 / \ 3 2 \ / \ 6 4 5
7Swap children of node 66Left=nil, Right=nil 1 / \ 3 2 \ / \ 6 4 5
8Mirror left subtree of node 6 (nil)nilNone 1 / \ 3 2 \ / \ 6 4 5
9Mirror right subtree of node 6 (nil)nilNone 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=5, Right=4 1 / \ 3 2 \ / \ 6 5 4
12Mirror left subtree of node 2 (node 5)5None yet 1 / \ 3 2 \ / \ 6 5 4
13Swap children of node 55Left=nil, Right=nil 1 / \ 3 2 \ / \ 6 5 4
14Mirror left subtree of node 5 (nil)nilNone 1 / \ 3 2 \ / \ 6 5 4
15Mirror right subtree of node 5 (nil)nilNone 1 / \ 3 2 \ / \ 6 5 4
16Mirror right subtree of node 2 (node 4)4None yet 1 / \ 3 2 \ / \ 6 5 4
17Swap children of node 44Left=nil, Right=nil 1 / \ 3 2 \ / \ 6 5 4
18Mirror left subtree of node 4 (nil)nilNone 1 / \ 3 2 \ / \ 6 5 4
19Mirror right subtree of node 4 (nil)nilNone 1 / \ 3 2 \ / \ 6 5 4
20Return mirrored tree1None 1 / \ 3 2 \ / \ 6 5 4
💡 All nodes visited and children swapped, recursion ends.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 11Final
noderoot=1root=1node=3node=2root=1
node.Left23nil53
node.Right32642
Tree StateOriginalRoot swapped childrenLeft subtree mirroredRight subtree mirroredFully mirrored
Key Moments - 3 Insights
Why do we swap children before recursive calls?
Swapping children first (see Step 2) ensures that when recursion happens, the left subtree is actually the original right subtree and vice versa, so the mirror is correctly formed.
What happens when a node is nil?
When node is nil (see Steps 5, 8, 9), the function returns immediately without changes, stopping recursion on that path.
Why do we assign node.Left, node.Right = mirror(node.Right), mirror(node.Left)?
This simultaneously swaps the children and recursively mirrors subtrees, as shown in Step 2 and throughout the recursion.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the left child of node 1 after Step 2?
ANode 4
BNode 2
CNode 3
DNode 5
💡 Hint
Check the 'Pointer Changes' and 'Tree State' columns at Step 2.
At which step does the function first visit node 6?
AStep 6
BStep 4
CStep 10
DStep 12
💡 Hint
Look for 'Node Visited' column in the execution table.
If the tree had no right child at root, what would happen at Step 2?
ALeft child becomes nil, right child stays the same
BLeft and right children swap, right becomes nil
CNo swap happens
DFunction returns nil immediately
💡 Hint
Refer to how swapping works in Step 2 with existing children.
Concept Snapshot
Mirror a Binary Tree:
- Swap left and right children of each node
- Recursively apply to subtrees
- Base case: nil node returns nil
- Result is a tree flipped around its root
- Code: node.Left, node.Right = mirror(node.Right), mirror(node.Left)
Full Transcript
To mirror a binary tree, start at the root node. Swap its left and right children. Then recursively do the same for the left and right subtrees. If a node is nil, return nil immediately. This process flips the tree around its root, creating a mirror image. The code swaps children first, then calls mirror recursively on the swapped children. This ensures the entire tree is mirrored correctly.