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 number of steps grow when the tree has more nodes?
Analyze the time complexity of the following code snippet.
void mirrorTree(Node* root) {
if (root == nullptr) return;
mirrorTree(root->left);
mirrorTree(root->right);
std::swap(root->left, root->right);
}
This code swaps the left and right children of every node in the tree, effectively mirroring it.
- Primary operation: Recursive calls visiting each node once and swapping children.
- How many times: Once per node in the tree.
Each node is visited once, so the total steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 swaps and recursive calls |
| 100 | About 100 swaps and recursive calls |
| 1000 | About 1000 swaps and recursive calls |
Pattern observation: The work grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to mirror the tree grows directly with the number of nodes in the tree.
[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 whole tree must be visited, not just the root.
Understanding this helps you explain how recursive tree algorithms scale, a key skill for many coding challenges.
"What if we changed the tree to be a linked list (all nodes have only one child)? How would the time complexity change?"
Practice
Solution
Step 1: Understand the mirror operation
Mirroring means swapping left and right children of every node in the tree.Step 2: Clarify what happens to nodes
Each node's left child becomes its right child and vice versa, recursively.Final Answer:
Swapping the left and right children of every node -> Option CQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means reversing node values
- Confusing mirror with tree traversal order
- Assuming mirror deletes nodes
Solution
Step 1: Identify correct parameter type
The function should take a pointer to the root node to modify the tree in place.Step 2: Identify correct return type
Since the tree is modified in place, the function can return void.Final Answer:
void mirror(Node* root) -> Option AQuick Check:
Pointer param + void return = void mirror(Node* root) [OK]
- Passing node by value instead of pointer
- Returning Node instead of void
- Using incorrect parameter types
struct Node {
int data;
Node* left;
Node* right;
};
void mirror(Node* root) {
if (!root) return;
mirror(root->left);
mirror(root->right);
Node* temp = root->left;
root->left = root->right;
root->right = temp;
}
// Tree before mirror:
// 1
// / \
// 2 3
// / \
// 4 5
Node* root = new Node{1, new Node{2, new Node{4, nullptr, nullptr}, nullptr}, new Node{3, nullptr, new Node{5, nullptr, nullptr}}};
mirror(root);
// Print inorder traversal
void inorder(Node* node) {
if (!node) return;
inorder(node->left);
std::cout << node->data << " ";
inorder(node->right);
}
inorder(root);Solution
Step 1: Understand the original tree and inorder traversal
Original inorder: left-root-right = 4 2 1 3 5Step 2: Mirror swaps left and right children recursively
Mirrored tree structure: 1 / \ 3 2 / \ 5 4 Inorder traversal now: 5 3 1 2 4Final Answer:
5 3 1 2 4 -> Option BQuick Check:
Inorder mirrored = 5 3 1 2 4 [OK]
- Not swapping children correctly
- Confusing preorder with inorder traversal
- Printing original tree instead of mirrored
void mirror(Node* root) {
if (root == nullptr) return;
mirror(root->left);
mirror(root->right);
Node* temp = root->left;
root->left = root->right;
root->right = temp;
return root;
}Solution
Step 1: Check function return type and usage
The function is declared void but tries to return root pointer, which causes a compile error.Step 2: Fix by removing the return statement
Since the tree is modified in place, no return is needed; remove 'return root;'.Final Answer:
Function should return void, remove 'return root;' -> Option DQuick Check:
Void function must not return value [OK]
- Returning value from void function
- Changing base case incorrectly
- Deleting nodes unnecessarily
Solution
Step 1: Understand counting swaps during recursion
Each node swap can be counted by returning 1 plus counts from left and right subtrees.Step 2: Modify mirror to return int and sum counts
Return 0 if node is null; else return 1 + mirror(left) + mirror(right).Final Answer:
Return the number of nodes from the function and sum left and right counts -> Option AQuick Check:
Return counts from recursion to sum swaps [OK]
- Using global variables causing side effects
- Counting nodes before and after mirroring (no change)
- Not accumulating counts correctly
Ready to prove it?
Pass the test and this pattern moves unknown
