Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does mirroring a binary tree mean?
easy
A. Sorting the nodes in ascending order
B. Reversing the order of nodes in each level
C. Swapping the left and right children of every node
D. Deleting all leaf nodes

Solution

  1. Step 1: Understand the mirror operation

    Mirroring means swapping left and right children of every node in the tree.
  2. Step 2: Clarify what happens to nodes

    Each node's left child becomes its right child and vice versa, recursively.
  3. Final Answer:

    Swapping the left and right children of every node -> Option C
  4. Quick Check:

    Mirror = Swap children [OK]
Hint: Mirror means swap left and right children at all nodes [OK]
Common Mistakes:
  • Thinking mirror means reversing node values
  • Confusing mirror with tree traversal order
  • Assuming mirror deletes nodes
2. Which of the following is the correct C++ function signature to mirror a binary tree node?
easy
A. void mirror(Node* root)
B. int mirror(Node root)
C. Node mirror(Node* root)
D. void mirror(Node root)

Solution

  1. Step 1: Identify correct parameter type

    The function should take a pointer to the root node to modify the tree in place.
  2. Step 2: Identify correct return type

    Since the tree is modified in place, the function can return void.
  3. Final Answer:

    void mirror(Node* root) -> Option A
  4. Quick Check:

    Pointer param + void return = void mirror(Node* root) [OK]
Hint: Use pointer parameter and void return for in-place tree changes [OK]
Common Mistakes:
  • Passing node by value instead of pointer
  • Returning Node instead of void
  • Using incorrect parameter types
3. What is the output of the following code after mirroring the binary tree?
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);
medium
A. 5 3 1 4 2
B. 5 3 1 2 4
C. 1 2 4 3 5
D. 4 2 1 3 5

Solution

  1. Step 1: Understand the original tree and inorder traversal

    Original inorder: left-root-right = 4 2 1 3 5
  2. Step 2: Mirror swaps left and right children recursively

    Mirrored tree structure: 1 / \ 3 2 / \ 5 4 Inorder traversal now: 5 3 1 2 4
  3. Final Answer:

    5 3 1 2 4 -> Option B
  4. Quick Check:

    Inorder mirrored = 5 3 1 2 4 [OK]
Hint: Mirror swaps children; inorder reverses left-right order [OK]
Common Mistakes:
  • Not swapping children correctly
  • Confusing preorder with inorder traversal
  • Printing original tree instead of mirrored
4. Identify the error in this mirror function and choose the fix:
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;
}
medium
A. Swap root->left and root->right before recursive calls
B. Change 'if (root == nullptr)' to 'if (!root->left && !root->right)'
C. Add 'delete temp;' after swapping
D. Function should return void, remove 'return root;'

Solution

  1. Step 1: Check function return type and usage

    The function is declared void but tries to return root pointer, which causes a compile error.
  2. Step 2: Fix by removing the return statement

    Since the tree is modified in place, no return is needed; remove 'return root;'.
  3. Final Answer:

    Function should return void, remove 'return root;' -> Option D
  4. Quick Check:

    Void function must not return value [OK]
Hint: Void functions should not return values [OK]
Common Mistakes:
  • Returning value from void function
  • Changing base case incorrectly
  • Deleting nodes unnecessarily
5. Given a binary tree, how would you modify the mirror function to also count how many nodes were swapped during the mirroring process?
hard
A. Return the number of nodes from the function and sum left and right counts
B. Add a counter parameter and increment it each time children are swapped
C. Use a global variable to count swaps inside the mirror function
D. Count nodes before and after mirroring and subtract counts

Solution

  1. Step 1: Understand counting swaps during recursion

    Each node swap can be counted by returning 1 plus counts from left and right subtrees.
  2. Step 2: Modify mirror to return int and sum counts

    Return 0 if node is null; else return 1 + mirror(left) + mirror(right).
  3. Final Answer:

    Return the number of nodes from the function and sum left and right counts -> Option A
  4. Quick Check:

    Return counts from recursion to sum swaps [OK]
Hint: Return counts from recursion to sum swapped nodes [OK]
Common Mistakes:
  • Using global variables causing side effects
  • Counting nodes before and after mirroring (no change)
  • Not accumulating counts correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard