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 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#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);

void Mirror(Node node) {
    if (node == null) return;
    Node temp = node.left;
    node.left = node.right;
    node.right = temp;
    Mirror(node.left);
    Mirror(node.right);
}

List<int> result = new List<int>();
void InOrder(Node node) {
    if (node == null) return;
    InOrder(node.left);
    result.Add(node.val);
    InOrder(node.right);
}

Mirror(root);
InOrder(root);
Console.WriteLine(string.Join(",", result));
A3,2,1
B3,1,2
C2,1,3
D1,3,2
Attempts:
2 left
💡 Hint
Remember that mirroring swaps left and right children at every node.
❓ Predict Output
intermediate
2:00remaining
Mirror a Binary Tree with Multiple Levels
What is the pre-order traversal output after mirroring this binary tree?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(7);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.left = new Node(6);
root.right.right = new Node(9);

void Mirror(Node node) {
    if (node == null) return;
    Node temp = node.left;
    node.left = node.right;
    node.right = temp;
    Mirror(node.left);
    Mirror(node.right);
}

List<int> result = new List<int>();
void PreOrder(Node node) {
    if (node == null) return;
    result.Add(node.val);
    PreOrder(node.left);
    PreOrder(node.right);
}

Mirror(root);
PreOrder(root);
Console.WriteLine(string.Join(",", result));
A4,7,9,6,2,3,1
B4,2,1,3,7,6,9
C4,7,6,9,2,1,3
D4,9,7,6,3,2,1
Attempts:
2 left
💡 Hint
Pre-order visits root, then left, then right. Mirroring swaps left and right children.
🔧 Debug
advanced
2:00remaining
Identify the Error in Mirror Function
What error will this mirror function cause when run on a binary tree?
DSA C#
void Mirror(Node node) {
    if (node == null) return;
    Node temp = node.left;
    node.left = node.right;
    node.right = temp;
    Mirror(node.right);
    Mirror(node.left);
}
ANo error; function works correctly
BStackOverflow due to infinite recursion
CNullReferenceException at runtime
DCompilation error due to wrong syntax
Attempts:
2 left
💡 Hint
Check the order of recursive calls after swapping children.
❓ Predict Output
advanced
2:00remaining
Output of Level-Order Traversal After Mirroring
Given the binary tree below, what is the output of the level-order traversal after mirroring?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.left = new Node(12);
root.right.right = new Node(18);

void Mirror(Node node) {
    if (node == null) return;
    Node temp = node.left;
    node.left = node.right;
    node.right = temp;
    Mirror(node.left);
    Mirror(node.right);
}

List<int> result = new List<int>();
Queue<Node> queue = new Queue<Node>();

Mirror(root);
queue.Enqueue(root);
while(queue.Count > 0) {
    Node current = queue.Dequeue();
    result.Add(current.val);
    if (current.left != null) queue.Enqueue(current.left);
    if (current.right != null) queue.Enqueue(current.right);
}

Console.WriteLine(string.Join(",", result));
A10,5,15,3,7,12,18
B10,5,15,7,3,18,12
C10,15,5,12,18,3,7
D10,15,5,18,12,7,3
Attempts:
2 left
💡 Hint
Level-order visits nodes level by level from left to right. Mirroring swaps children at every node.
🧠 Conceptual
expert
2:00remaining
Effect of Mirroring Twice on a Binary Tree
What is the result of applying the mirror operation twice on any binary tree?
AThe tree becomes a binary search tree
BThe tree becomes a complete binary tree
CThe tree remains unchanged, identical to the original
DThe tree becomes a linked list
Attempts:
2 left
💡 Hint
Think about what happens when you swap left and right children twice.

Practice

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

Solution

  1. Step 1: Understand the mirror operation

    Mirroring means swapping left and right children at every node in the tree.
  2. Step 2: Identify the effect on the tree

    This operation flips the tree horizontally, creating a mirror image.
  3. Final Answer:

    Swapping the left and right children of every node -> Option A
  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 C# method signatures correctly represents a recursive function to mirror a binary tree node named root?
easy
A. void MirrorTree(Node root)
B. int MirrorTree(Node root)
C. Node MirrorTree(Node root)
D. bool MirrorTree(Node root)

Solution

  1. Step 1: Understand the purpose of the function

    The function modifies the tree in place and does not need to return a value.
  2. Step 2: Choose the correct return type

    Since it changes the tree nodes directly, the return type should be void.
  3. Final Answer:

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

    In-place change means void return [OK]
Hint: Mirror function usually returns void as it modifies tree directly [OK]
Common Mistakes:
  • Returning int or bool unnecessarily
  • Returning Node when not needed
  • Using incorrect parameter types
3. Given the following binary tree and the C# code to mirror it, what will be the output of the inorder traversal after mirroring?

    1
   / \
  2   3
 /     \
4       5

void MirrorTree(Node root) {
  if (root == null) return;
  var temp = root.left;
  root.left = root.right;
  root.right = temp;
  MirrorTree(root.left);
  MirrorTree(root.right);
}

// Inorder traversal prints nodes in order: left, root, right
medium
A. [3, 5, 1, 4, 2]
B. [4, 2, 1, 3, 5]
C. [5, 3, 1, 2, 4]
D. [1, 2, 4, 3, 5]

Solution

  1. Step 1: Mirror the tree structure

    Swap left and right children at every node:
    Original root 1: left=2, right=3 -> after mirror left=3, right=2
    Node 2: left=4, right=null -> after mirror left=null, right=4
    Node 3: left=null, right=5 -> after mirror left=5, right=null
  2. Step 2: Perform inorder traversal on mirrored tree

    Inorder = left, root, right
    Start at root (1):
    Left subtree (3): left=5, right=null -> inorder: 5,3
    Root: 1
    Right subtree (2): left=null, right=4 -> inorder: 2,4
    Combined inorder: [5, 3, 1, 2, 4]
  3. Final Answer:

    [5, 3, 1, 2, 4] -> Option C
  4. Quick Check:

    Mirror + inorder = [5,3,1,2,4] [OK]
Hint: Mirror swaps children; inorder visits left-root-right [OK]
Common Mistakes:
  • Not swapping children correctly
  • Confusing preorder with inorder traversal
  • Forgetting to mirror all nodes
4. The following C# code attempts to mirror a binary tree but does not work correctly. What is the main error?

void MirrorTree(Node root) {
  MirrorTree(root.left);
  MirrorTree(root.right);
  var temp = root.left;
  root.left = root.right;
  root.right = temp;
}
medium
A. Swapping children happens after recursive calls, causing incorrect mirror
B. Missing base case for null root
C. Using var instead of explicit type causes error
D. Function should return Node instead of void

Solution

  1. Step 1: Analyze the base case

    The code lacks an if (root == null) return; check before recursing on children.
  2. Step 2: Understand why this causes error

    Recursing on null children leads to NullReferenceException or stack overflow since there's no termination condition.
  3. Final Answer:

    Missing base case for null root -> Option B
  4. Quick Check:

    Recursion needs if(root == null) return; [OK]
Hint: Always include null base case in tree recursion [OK]
Common Mistakes:
  • Omitting base case for null nodes
  • Swapping after recursion instead of before
  • Thinking var causes error
5. You have a binary tree where some nodes have only one child (either left or right). You want to mirror this tree using C#. Which approach correctly handles all cases including nodes with single children?
hard
A. Mirror by creating a new tree instead of modifying original
B. Only swap children if both left and right children exist
C. Swap children only at leaf nodes
D. Recursively swap left and right children if node is not null, then recurse on both children

Solution

  1. Step 1: Consider nodes with single child

    Nodes with only one child still need swapping; swapping null with a child is valid.
  2. Step 2: Choose recursive approach that swaps and recurses regardless of child presence

    Swapping children even if one is null ensures correct mirror. Then recurse on both children to continue mirroring.
  3. Final Answer:

    Recursively swap left and right children if node is not null, then recurse on both children -> Option D
  4. Quick Check:

    Swap children always, recurse both sides [OK]
Hint: Always swap children even if one is null, then recurse [OK]
Common Mistakes:
  • Skipping swap when one child is null
  • Swapping only leaf nodes
  • Creating new tree unnecessarily
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