0
0
DSA C++programming~20 mins

Mirror a Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A[5, 2, 4, 1, 3]
B[4, 2, 5, 1, 3]
C[3, 1, 4, 2, 5]
D[3, 1, 5, 2, 4]
Attempts:
2 left
💡 Hint
Remember that mirroring swaps left and right children at every node.
🧠 Conceptual
intermediate
1:30remaining
Understanding Mirror Operation on Binary Trees
Which statement correctly describes the effect of the mirror operation on a binary tree?
AIt swaps the left and right children of every node in the tree.
BIt reverses the values of nodes in the tree without changing structure.
CIt removes all leaf nodes from the tree.
DIt converts the binary tree into a linked list.
Attempts:
2 left
💡 Hint
Think about what happens to the children of each node during mirroring.
🔧 Debug
advanced
2: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;
}
AMemory leak due to not deleting nodes.
BLogical error: setting root to nullptr does not affect the tree structure.
CRuntime error: null pointer dereference.
DNo error; function works correctly.
Attempts:
2 left
💡 Hint
Consider what setting root = nullptr inside the function does.
Predict Output
advanced
2: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;
}
A[10, 20, 40, 50, 30, 60]
B[10, 30, 20, 60, 50, 40]
C[10, 30, 60, 20, 50, 40]
D[10, 20, 50, 40, 30, 60]
Attempts:
2 left
💡 Hint
Pre-order traversal visits root, then left subtree, then right subtree after mirroring.
🧠 Conceptual
expert
1:30remaining
Effect of Mirroring on Tree Height and Structure
Which statement about the height and structure of a binary tree after mirroring is true?
AMirroring does not change the height but reverses the left and right subtrees at every node.
BMirroring changes the height of the tree but keeps the node values in the same order.
CMirroring flattens the tree into a linked list increasing the height.
DMirroring duplicates all nodes, doubling the tree size.
Attempts:
2 left
💡 Hint
Think about what swapping left and right children does to the tree shape and height.