Challenge - 5 Problems
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of 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;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
void mirror(Node* root) {
if (!root) return;
mirror(root->left);
mirror(root->right);
Node* temp = root->left;
root->left = root->right;
root->right = temp;
}
void inorder(Node* root, std::vector<int>& result) {
if (!root) return;
inorder(root->left, result);
result.push_back(root->data);
inorder(root->right, result);
}
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
mirror(root);
std::vector<int> result;
inorder(root, result);
for (int val : result) std::cout << val << " ";
return 0;
}Attempts:
2 left
💡 Hint
Remember that mirroring swaps left and right children at every node.
✗ Incorrect
Mirroring swaps left and right children recursively. The original in-order traversal is [4, 2, 5, 1, 3]. After mirroring, the tree structure changes and the in-order traversal becomes [3, 1, 5, 2, 4].
🧠 Conceptual
intermediate1:30remaining
Understanding Mirror Operation on Binary Trees
Which statement correctly describes the effect of the mirror operation on a binary tree?
Attempts:
2 left
💡 Hint
Think about what happens to the children of each node during mirroring.
✗ Incorrect
Mirroring a binary tree means swapping the left and right children of every node recursively, effectively creating a mirror image of the tree structure.
🔧 Debug
advanced2:00remaining
Identify the Bug in Mirror Function
What error will occur when running this mirror function code?
DSA C++
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;
root = nullptr;
}Attempts:
2 left
💡 Hint
Consider what setting root = nullptr inside the function does.
✗ Incorrect
Setting root = nullptr only changes the local pointer variable and does not affect the actual tree nodes or structure. This line is unnecessary and misleading but does not cause a runtime error.
❓ Predict Output
advanced2:30remaining
Output After Mirroring a Complex Tree
Given the following tree and mirror function, what is the pre-order traversal output after mirroring?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
void mirror(Node* root) {
if (!root) return;
mirror(root->left);
mirror(root->right);
Node* temp = root->left;
root->left = root->right;
root->right = temp;
}
void preorder(Node* root, std::vector<int>& result) {
if (!root) return;
result.push_back(root->data);
preorder(root->left, result);
preorder(root->right, result);
}
int main() {
Node* root = new Node(10);
root->left = new Node(20);
root->right = new Node(30);
root->left->left = new Node(40);
root->left->right = new Node(50);
root->right->right = new Node(60);
mirror(root);
std::vector<int> result;
preorder(root, result);
for (int val : result) std::cout << val << " ";
return 0;
}Attempts:
2 left
💡 Hint
Pre-order traversal visits root, then left subtree, then right subtree after mirroring.
✗ Incorrect
After mirroring, left and right children are swapped at every node. The pre-order traversal visits nodes in the order: root, mirrored left subtree (original right), mirrored right subtree (original left).
🧠 Conceptual
expert1:30remaining
Effect of Mirroring on Tree Height and Structure
Which statement about the height and structure of a binary tree after mirroring is true?
Attempts:
2 left
💡 Hint
Think about what swapping left and right children does to the tree shape and height.
✗ Incorrect
Mirroring swaps left and right children at every node, which does not change the height of the tree but reverses the structure horizontally.