Challenge - 5 Problems
Mirror Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after mirroring a simple binary tree
What is the output of the in-order traversal after mirroring the given binary tree?
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void mirror(struct Node* root) {
if (root == NULL) return;
struct Node* temp;
mirror(root->left);
mirror(root->right);
temp = root->left;
root->left = root->right;
root->right = temp;
}
void inorder(struct Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
int main() {
struct Node n1 = {1, NULL, NULL};
struct Node n2 = {2, NULL, NULL};
struct Node n3 = {3, &n1, &n2};
mirror(&n3);
inorder(&n3);
return 0;
}Attempts:
2 left
💡 Hint
Remember that mirroring swaps left and right children at every node.
✗ Incorrect
Initially, the tree root 3 has left child 1 and right child 2. After mirroring, left child becomes 2 and right child becomes 1. In-order traversal visits left, root, right, so output is 2 3 1.
🧠 Conceptual
intermediateUnderstanding the mirror operation effect
After mirroring a binary tree, which of the following statements is always true?
Attempts:
2 left
💡 Hint
Think about what swapping left and right children means for each node.
✗ Incorrect
Mirroring swaps the left and right children of every node, so the left subtree becomes the right subtree and vice versa. It does not guarantee binary search tree properties or change height systematically.
🔧 Debug
advancedIdentify the bug in this mirror function
What error will this mirror function cause when run on a binary tree?
DSA C
void mirror(struct Node* root) {
if (root == NULL) return;
struct Node* temp;
mirror(root->left);
mirror(root->right);
temp = root->left;
root->left = root->right;
root->right = temp;
mirror(root->left);
}Attempts:
2 left
💡 Hint
Look carefully at the recursive calls and their order.
✗ Incorrect
The function calls mirror(root->left) twice: once before swapping and once after swapping. After swapping, root->left points to the original right subtree, so the second call causes infinite recursion.
❓ Predict Output
advancedOutput of level-order traversal after mirroring
Given the binary tree below, what is the output of the level-order traversal after mirroring?
Tree:
4
/ \
2 7
/ \ / \
1 3 6 9
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void mirror(struct Node* root) {
if (root == NULL) return;
struct Node* temp;
mirror(root->left);
mirror(root->right);
temp = root->left;
root->left = root->right;
root->right = temp;
}
void levelOrder(struct Node* root) {
if (root == NULL) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
struct Node* curr = queue[front++];
printf("%d ", curr->data);
if (curr->left) queue[rear++] = curr->left;
if (curr->right) queue[rear++] = curr->right;
}
}
int main() {
struct Node n1 = {1, NULL, NULL};
struct Node n3 = {3, NULL, NULL};
struct Node n6 = {6, NULL, NULL};
struct Node n9 = {9, NULL, NULL};
struct Node n2 = {2, &n1, &n3};
struct Node n7 = {7, &n6, &n9};
struct Node n4 = {4, &n2, &n7};
mirror(&n4);
levelOrder(&n4);
return 0;
}Attempts:
2 left
💡 Hint
Mirroring swaps left and right children at every node before level-order traversal.
✗ Incorrect
After mirroring, the tree becomes:
4
/ \
7 2
/ \ / \
9 6 3 1
Level-order visits nodes level by level: 4, then 7 and 2, then 9, 6, 3, 1.
🧠 Conceptual
expertEffect of mirroring on tree traversal orders
Which traversal order of the mirrored binary tree corresponds exactly to the reverse of the original tree's traversal order?
Attempts:
2 left
💡 Hint
Think about how post-order traversal visits nodes and how mirroring affects left and right subtrees.
✗ Incorrect
Mirroring swaps left and right children. The post-order traversal of the mirrored tree is the reverse of the original tree's post-order traversal.
