Mirror a Binary Tree in DSA C# - Time & Space Complexity
We want to understand how long it takes to flip a binary tree so its left and right children swap places.
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
public void MirrorTree(Node root) {
if (root == null) return;
MirrorTree(root.Left);
MirrorTree(root.Right);
Node temp = root.Left;
root.Left = root.Right;
root.Right = temp;
}
This code flips the tree by swapping left and right children for every node recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
Each node is visited once to swap its children, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 swaps |
| 100 | About 100 swaps |
| 1000 | About 1000 swaps |
Pattern observation: The time grows linearly as the tree size increases.
Time Complexity: O(n)
This means the time to mirror the tree grows directly with the number of nodes.
[X] Wrong: "The time complexity is O(n²) because of the recursive calls inside each other."
[OK] Correct: Each node is visited only once, so the total work is proportional to the number of nodes, not multiplied by itself.
Understanding this helps you explain how recursion visits all parts of a tree efficiently, a key skill in many tree problems.
"What if we changed the code to use an iterative approach with a stack? How would the time complexity change?"
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
