0
0
DSA C++programming~10 mins

Mirror a Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Mirror a Binary Tree
Start at root node
Swap left and right child
Recursively mirror left subtree
Recursively mirror right subtree
Return mirrored subtree
Start at the root, swap its left and right children, then do the same for each subtree recursively until all nodes are processed.
Execution Sample
DSA C++
void mirror(Node* root) {
  if (!root) return;
  swap(root->left, root->right);
  mirror(root->left);
  mirror(root->right);
}
This code 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
3Recurse left subtree3None3 / \ 6 7
4Swap children3left=7, right=63 / \ 7 6
5Recurse left subtree7None7 (leaf)
6Recurse right subtree6None6 (leaf)
7Recurse right subtree of root2None2 / \ 4 5
8Swap children2left=5, right=42 / \ 5 4
9Recurse left subtree5None5 (leaf)
10Recurse right subtree4None4 (leaf)
11Return to root1None1 / \ 3 2 / \ / \ 7 6 5 4
💡 All nodes visited and children swapped, tree is fully mirrored.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 8Final
rootNode 1Node 1Node 1Node 1Node 1
current nodeNode 1Node 3Node 7Node 2Node 1
left child of currentNode 2Node 7NoneNode 5Node 3
right child of currentNode 3Node 6NoneNode 4Node 2
tree structure1->2,3; 2->4,5; 3->6,71->3,2; 3->7,6; 2->4,53->7,6; 7 leaf; 6 leaf2->5,4; 5 leaf; 4 leaf1->3,2; 3->7,6; 2->5,4
Key Moments - 3 Insights
Why do we swap children before recursive calls?
Swapping children first (see Step 2) ensures that when recursion goes to left and right subtrees, it processes the new mirrored positions, correctly flipping the whole tree.
What happens when a node is a leaf?
At leaf nodes (Steps 5, 6, 9, 10), there are no children to swap, so recursion returns immediately, stopping further calls.
Why do we check if the node is null at the start?
The base case (Step 1) prevents recursion on empty nodes, avoiding errors and stopping recursion when reaching beyond leaves.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the left child of node 1 after Step 2?
ANode 2
BNode 3
CNode 4
DNode 5
💡 Hint
Check the 'Pointer Changes' and 'Tree State' columns at Step 2.
At which step does the recursion visit node 7?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the 'Node Visited' column to find when node 7 is processed.
If we skip swapping children at each node, how would the final tree state change?
ATree becomes empty
BOnly leaf nodes swap
CTree remains unchanged
DTree becomes a linked list
💡 Hint
Refer to the 'Operation' column and understand the role of swapping in mirroring.
Concept Snapshot
Mirror a Binary Tree:
- Swap left and right child of each node
- Recursively apply to left and right subtrees
- Base case: if node is null, return
- Result: tree flipped around its root
- Common use: image reflection, tree transformations
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 subtree and the right subtree. If a node is null, stop recursion. This process flips the tree around its root, creating a mirror image. The code swaps children first, then recurses, ensuring the entire tree is mirrored correctly. Leaf nodes have no children to swap, so recursion ends there. This method is simple and effective for tree transformations.