The problem can be found at the following link: Question Link
Given a binary tree, convert it into its Mirror Tree by swapping the left and right children of all non-leaf nodes.
1
/ \
2 3
/
4
1
/ \
3 2
\
4
![](https://private-user-images.githubusercontent.com/124852522/409841856-16c6b8d7-160f-4260-b6e5-629d51b3d248.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5OTQ4NzksIm5iZiI6MTczODk5NDU3OSwicGF0aCI6Ii8xMjQ4NTI1MjIvNDA5ODQxODU2LTE2YzZiOGQ3LTE2MGYtNDI2MC1iNmU1LTYyOWQ1MWIzZDI0OC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA4JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwOFQwNjAyNTlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01MGFmODZlOTI1YjIxOGZmZDEzZmI0N2Q4NjdlM2QyMzViYzg2MWIzN2JjMmExOTNmMmQwY2Y2NzY3M2E5ZDRhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.1Yn_NhZeyMLVckeFZoPexZyXfo1Qskno7lOzwKSvYqs)
Every non-leaf node has its left and right children interchanged.
1
/ \
2 3
/ \
4 5
1
/ \
3 2
/ \
5 4
![](https://private-user-images.githubusercontent.com/124852522/409841393-f4d620f5-19e1-4c84-94a5-a543cb89f9d1.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5OTQ4NzksIm5iZiI6MTczODk5NDU3OSwicGF0aCI6Ii8xMjQ4NTI1MjIvNDA5ODQxMzkzLWY0ZDYyMGY1LTE5ZTEtNGM4NC05NGE1LWE1NDNjYjg5ZjlkMS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA4JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwOFQwNjAyNTlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jZDY5OTM4YTk2OTY1YWU0OTk0NzFiYmJlMTQ5MTE3MWZjNzE3OTQ0MGY5Y2M2ODRkMWFjOTczNTYxNjU1N2M4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.4YHsSGble3EnBKXpImjxqBLouNdlU9Tr-kh3KPD0Z88)
Every non-leaf node has its left and right children interchanged.
- 1 ≤ number of nodes ≤
$10^5$ - 1 ≤ node->data ≤
$10^5$
- Base Case: If the node is
NULL
, return. - Recursively traverse the left and right subtrees.
- Swap the left and right children of the current node.
- Expected Time Complexity:
O(N)
, since every node is visited once. - Expected Auxiliary Space Complexity:
O(1)
ORO(H)
for recursive DFS (H = height of the tree
).
void mirror(Node *n) {
if (!n) return;
mirror(n->left);
mirror(n->right);
Node* t = n->left;
n->left = n->right;
n->right = t;
}
Note: The C code may show an error when compiled and run, but if you proceed with submission, it still passes all test cases.
For example, consider the input:
1 2 3 N N 4
The output after compilation and running:
1 3 2
Expected output:
1 3 2 N 4
Although there is a difference in output format during execution, submitting the code results in a successful pass for all test cases.
class Solution {
public:
void mirror(Node* node) {
if (!node) return;
mirror(node->left);
mirror(node->right);
swap(node->left, node->right);
}
};
class Solution {
public:
void mirror(Node* root) {
if (!root) return;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* cur = q.front(); q.pop();
swap(cur->left, cur->right);
if (cur->left) q.push(cur->left);
if (cur->right) q.push(cur->right);
}
}
};
class Solution {
public:
void mirror(Node* root) {
if (!root) return;
stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* cur = s.top(); s.pop();
swap(cur->left, cur->right);
if (cur->left) s.push(cur->left);
if (cur->right) s.push(cur->right);
}
}
};
Approach | Time Complexity | Space Complexity | Method | Pros | Cons |
---|---|---|---|---|---|
Recursive DFS (Top-Down) | 🟢 O(N) | 🟡 O(H) | Recursion | Simple and concise | May cause stack overflow for deep trees |
Iterative BFS (Level Order) | 🟢 O(N) | 🔴 O(W) | Queue-based | Avoids recursion depth issues | Uses more memory for wide trees |
Iterative DFS (Stack) | 🟢 O(N) | 🟡 O(H) | Stack-based | Explicit control over traversal order | Still uses extra space for the stack |
- For balanced trees, the Recursive DFS approach is fine.
- For deep trees, the Iterative BFS approach is preferable to avoid recursion depth issues.
- For explicit control and iterative processing, the Iterative DFS (Stack) approach is a solid option.
class Solution {
void mirror(Node node) {
if (node == null) return;
mirror(node.left);
mirror(node.right);
Node temp = node.left;
node.left = node.right;
node.right = temp;
}
}
class Solution:
def mirror(self, root):
if not root:
return
self.mirror(root.left)
self.mirror(root.right)
root.left, root.right = root.right, root.left
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐