Mirror a Binary Tree in DSA C - Time & Space Complexity
We want to understand how the time needed to mirror a binary tree changes as the tree grows.
How does the work increase when the tree has more nodes?
Analyze the time complexity of the following code snippet.
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 swaps the left and right children of every node in the tree, effectively mirroring it.
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 work grows linearly as the tree size increases.
Time Complexity: O(n)
This means the time to mirror the tree grows in direct proportion to the number of nodes.
[X] Wrong: "Mirroring only swaps the root's children, so it takes constant time."
[OK] Correct: Every node's children must be swapped, so the entire tree must be visited, not just the root.
Understanding this helps you explain how recursive tree operations scale, a common skill in coding interviews.
"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 a binary tree means swapping the left and right children of every node.Step 2: Identify the effect on the tree
This operation flips the tree around its center, creating a mirror image.Final Answer:
Swapping the left and right children of every node in the tree -> Option BQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means sorting nodes
- Confusing mirror with deleting nodes
- Assuming mirror counts nodes
Solution
Step 1: Understand parameter passing for trees in C
Binary trees are usually passed as pointers to nodes to allow modification.Step 2: Identify correct return type
Mirroring modifies the tree in place, so the function returns void.Final Answer:
void mirrorTree(struct Node* root); -> Option AQuick Check:
Pointer parameter + void return = void mirrorTree(struct Node* root); [OK]
- Passing struct Node by value instead of pointer
- Returning struct Node instead of void
- Using int return type incorrectly
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void mirrorTree(struct Node* root) {
if (root == NULL) return;
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->left);
mirrorTree(root->right);
}
// Tree before mirror:
// 1
// / \
// 2 3
// / \
// 4 5
// After mirror, print preorder traversal:
// Expected output?
Solution
Step 1: Understand the mirror operation on the tree
Swapping left and right children at every node flips the tree: Original root 1 has left=2, right=3 After mirror: left=3, right=2 Node 2 has left=4, right=NULL After mirror: left=NULL, right=4 Node 3 has left=NULL, right=5 After mirror: left=5, right=NULLStep 2: Perform preorder traversal on mirrored tree
Preorder: root, left, right Visit 1 Go left to 3 Go left to 5 (leaf) Back to 1, go right to 2 Go left NULL, go right 4 Sequence: 1 3 5 2 4Final Answer:
1 3 5 2 4 -> Option DQuick Check:
Mirror + preorder = 1 3 5 2 4 [OK]
- Not swapping children correctly
- Confusing preorder with inorder or postorder
- Missing leaf nodes in traversal
void mirrorTree(struct Node* root) {
if (root == NULL) return;
mirrorTree(root->left);
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->right);
}Solution
Step 1: Analyze the order of operations in the function
The function calls mirrorTree on left and right children before swapping them. This causes the recursive calls to work on original children, not swapped ones.Step 2: Correct the order to swap first, then recurse
Swapping children first ensures recursive calls mirror the swapped subtrees correctly.Final Answer:
Swap children before recursive calls to mirrorTree -> Option CQuick Check:
Swap first, recurse later [OK]
- Swapping after recursion causes incorrect mirror
- Removing base case causes crashes
- Changing temp type breaks pointer logic
Solution
Step 1: Understand mirroring effect on node positions
Mirroring swaps left and right children but does not add or remove nodes. Leaf nodes remain leaf nodes but their positions flip left to right.Step 2: Confirm leaf nodes count and structure
Number of leaf nodes stays the same; no nodes become internal or are removed. Only their left-right positions change.Final Answer:
All leaf nodes remain leaf nodes but their positions are mirrored -> Option AQuick Check:
Mirror flips positions, leaf count unchanged [OK]
- Thinking leaf nodes become internal nodes
- Assuming nodes are added or removed
- Believing leaf count changes
Ready to prove it?
Pass the test and this pattern moves unknown
