What if you could flip a whole tree upside down with just a few lines of code?
Why Mirror a Binary Tree in DSA C#?
Imagine you have a family tree drawn on paper, and you want to see its reflection in a mirror. Doing this by hand means redrawing every branch and leaf in reverse order, which is tiring and easy to mess up.
Manually flipping each branch and node is slow and confusing. You might forget to swap some parts or draw them incorrectly, leading to mistakes and wasted time.
Mirroring a binary tree with code swaps the left and right children of every node automatically. This simple process quickly creates the mirror image without any guesswork or errors.
void MirrorTree(Node root) {
// Manually swap left and right for each node
if (root == null) return;
var temp = root.left;
root.left = root.right;
root.right = temp;
MirrorTree(root.left);
MirrorTree(root.right);
}void MirrorTree(Node root) {
if (root == null) return;
(root.left, root.right) = (root.right, root.left);
MirrorTree(root.left);
MirrorTree(root.right);
}It lets you quickly create the mirror image of any tree structure, which is useful in graphics, data transformations, and problem solving.
In photo editing apps, flipping images horizontally is like mirroring a tree structure of pixels; this algorithm helps computers do that efficiently.
Manual flipping is slow and error-prone.
Mirroring swaps children nodes recursively.
It enables quick and reliable tree reflections.
Practice
Solution
Step 1: Understand the mirror operation
Mirroring means swapping left and right children at every node in the tree.Step 2: Identify the effect on the tree
This operation flips the tree horizontally, creating a mirror image.Final Answer:
Swapping the left and right children of every node -> Option AQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means sorting nodes
- Confusing mirror with deleting nodes
- Assuming mirror counts nodes
root?Solution
Step 1: Understand the purpose of the function
The function modifies the tree in place and does not need to return a value.Step 2: Choose the correct return type
Since it changes the tree nodes directly, the return type should be void.Final Answer:
void MirrorTree(Node root) -> Option AQuick Check:
In-place change means void return [OK]
- Returning int or bool unnecessarily
- Returning Node when not needed
- Using incorrect parameter types
1
/ \
2 3
/ \
4 5
void MirrorTree(Node root) {
if (root == null) return;
var temp = root.left;
root.left = root.right;
root.right = temp;
MirrorTree(root.left);
MirrorTree(root.right);
}
// Inorder traversal prints nodes in order: left, root, right
Solution
Step 1: Mirror the tree structure
Swap left and right children at every node:
Original root 1: left=2, right=3 -> after mirror left=3, right=2
Node 2: left=4, right=null -> after mirror left=null, right=4
Node 3: left=null, right=5 -> after mirror left=5, right=nullStep 2: Perform inorder traversal on mirrored tree
Inorder = left, root, right
Start at root (1):
Left subtree (3): left=5, right=null -> inorder: 5,3
Root: 1
Right subtree (2): left=null, right=4 -> inorder: 2,4
Combined inorder: [5, 3, 1, 2, 4]Final Answer:
[5, 3, 1, 2, 4] -> Option CQuick Check:
Mirror + inorder = [5,3,1,2,4] [OK]
- Not swapping children correctly
- Confusing preorder with inorder traversal
- Forgetting to mirror all nodes
void MirrorTree(Node root) {
MirrorTree(root.left);
MirrorTree(root.right);
var temp = root.left;
root.left = root.right;
root.right = temp;
}Solution
Step 1: Analyze the base case
The code lacks an if (root == null) return; check before recursing on children.Step 2: Understand why this causes error
Recursing on null children leads to NullReferenceException or stack overflow since there's no termination condition.Final Answer:
Missing base case for null root -> Option BQuick Check:
Recursion needs if(root == null) return; [OK]
- Omitting base case for null nodes
- Swapping after recursion instead of before
- Thinking var causes error
Solution
Step 1: Consider nodes with single child
Nodes with only one child still need swapping; swapping null with a child is valid.Step 2: Choose recursive approach that swaps and recurses regardless of child presence
Swapping children even if one is null ensures correct mirror. Then recurse on both children to continue mirroring.Final Answer:
Recursively swap left and right children if node is not null, then recurse on both children -> Option DQuick Check:
Swap children always, recurse both sides [OK]
- Skipping swap when one child is null
- Swapping only leaf nodes
- Creating new tree unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
