Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mirror Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
A3 2 1
B1 3 2
C2 3 1
D2 1 3
Attempts:
2 left
💡 Hint
Remember that mirroring swaps left and right children at every node.
🧠 Conceptual
intermediate
1:30remaining
Understanding the mirror operation effect
After mirroring a binary tree, which of the following statements is always true?
AThe left subtree becomes the right subtree and vice versa at every node.
BThe tree becomes a binary search tree.
CThe height of the tree increases by one.
DAll leaf nodes become internal nodes.
Attempts:
2 left
💡 Hint
Think about what swapping left and right children means for each node.
🔧 Debug
advanced
2:00remaining
Identify 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);
}
AStack overflow due to infinite recursion
BNo error, works correctly
CNull pointer dereference
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Look carefully at the recursive calls and their order.
❓ Predict Output
advanced
2:30remaining
Output 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;
}
A4 2 7 3 1 9 6
B4 2 7 1 3 6 9
C4 7 2 6 9 1 3
D4 7 2 9 6 3 1
Attempts:
2 left
💡 Hint
Mirroring swaps left and right children at every node before level-order traversal.
🧠 Conceptual
expert
2:00remaining
Effect 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?
ALevel-order traversal
BPost-order traversal
CPre-order traversal
DIn-order traversal
Attempts:
2 left
💡 Hint
Think about how post-order traversal visits nodes and how mirroring affects left and right subtrees.

Practice

(1/5)
1. What does mirroring a binary tree mean?
easy
A. Sorting the nodes in ascending order
B. Swapping the left and right children of every node in the tree
C. Removing all leaf nodes from the tree
D. Counting the total number of nodes in the tree

Solution

  1. Step 1: Understand the mirror operation

    Mirroring a binary tree means swapping the left and right children of every node.
  2. Step 2: Identify the effect on the tree

    This operation flips the tree around its center, creating a mirror image.
  3. Final Answer:

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

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

Solution

  1. Step 1: Understand parameter passing for trees in C

    Binary trees are usually passed as pointers to nodes to allow modification.
  2. Step 2: Identify correct return type

    Mirroring modifies the tree in place, so the function returns void.
  3. Final Answer:

    void mirrorTree(struct Node* root); -> Option A
  4. Quick Check:

    Pointer parameter + void return = void mirrorTree(struct Node* root); [OK]
Hint: Use pointer to root and void return for in-place mirror [OK]
Common Mistakes:
  • Passing struct Node by value instead of pointer
  • Returning struct Node instead of void
  • Using int return type incorrectly
3. What will be the output of the following code snippet after mirroring the binary tree?
struct Node {
  int data;
  struct Node* left;
  struct Node* right;
};

void mirrorTree(struct Node* root) {
  if (root == NULL) return;
  struct Node* temp = root->left;
  root->left = root->right;
  root->right = temp;
  mirrorTree(root->left);
  mirrorTree(root->right);
}

// Tree before mirror:
//     1
//    / \
//   2   3
//  /     \
// 4       5

// After mirror, print preorder traversal:
// Expected output?
medium
A. 1 2 3 4 5
B. 1 2 4 3 5
C. 1 3 2 5 4
D. 1 3 5 2 4

Solution

  1. Step 1: Understand the mirror operation on the tree

    Swapping left and right children at every node flips the tree: Original root 1 has left=2, right=3 After mirror: left=3, right=2 Node 2 has left=4, right=NULL After mirror: left=NULL, right=4 Node 3 has left=NULL, right=5 After mirror: left=5, right=NULL
  2. Step 2: Perform preorder traversal on mirrored tree

    Preorder: root, left, right Visit 1 Go left to 3 Go left to 5 (leaf) Back to 1, go right to 2 Go left NULL, go right 4 Sequence: 1 3 5 2 4
  3. Final Answer:

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

    Mirror + preorder = 1 3 5 2 4 [OK]
Hint: Mirror swaps children; preorder visits root-left-right [OK]
Common Mistakes:
  • Not swapping children correctly
  • Confusing preorder with inorder or postorder
  • Missing leaf nodes in traversal
4. Identify the error in the following mirror function and select the fix:
void mirrorTree(struct Node* root) {
  if (root == NULL) return;
  mirrorTree(root->left);
  struct Node* temp = root->left;
  root->left = root->right;
  root->right = temp;
  mirrorTree(root->right);
}
medium
A. Add return type as int instead of void
B. Remove the base case check for NULL
C. Swap children before recursive calls to mirrorTree
D. Change temp to int instead of struct Node*

Solution

  1. Step 1: Analyze the order of operations in the function

    The function calls mirrorTree on left and right children before swapping them. This causes the recursive calls to work on original children, not swapped ones.
  2. Step 2: Correct the order to swap first, then recurse

    Swapping children first ensures recursive calls mirror the swapped subtrees correctly.
  3. Final Answer:

    Swap children before recursive calls to mirrorTree -> Option C
  4. Quick Check:

    Swap first, recurse later [OK]
Hint: Swap children before recursion to mirror correctly [OK]
Common Mistakes:
  • Swapping after recursion causes incorrect mirror
  • Removing base case causes crashes
  • Changing temp type breaks pointer logic
5. Given a binary tree where some nodes have only one child, what is the effect of mirroring the tree on the structure and leaf nodes? Choose the correct statement.
hard
A. All leaf nodes remain leaf nodes but their positions are mirrored
B. Some leaf nodes become internal nodes after mirroring
C. The number of leaf nodes doubles after mirroring
D. Mirroring removes all nodes with only one child

Solution

  1. Step 1: Understand mirroring effect on node positions

    Mirroring swaps left and right children but does not add or remove nodes. Leaf nodes remain leaf nodes but their positions flip left to right.
  2. Step 2: Confirm leaf nodes count and structure

    Number of leaf nodes stays the same; no nodes become internal or are removed. Only their left-right positions change.
  3. Final Answer:

    All leaf nodes remain leaf nodes but their positions are mirrored -> Option A
  4. Quick Check:

    Mirror flips positions, leaf count unchanged [OK]
Hint: Mirror flips positions, leaf nodes stay leaf nodes [OK]
Common Mistakes:
  • Thinking leaf nodes become internal nodes
  • Assuming nodes are added or removed
  • Believing leaf count changes
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