Mirroring a binary tree means swapping the left and right children of every node in the tree, creating a mirror image of the original tree.
Click to reveal answer
beginner
What is the base case when recursively mirroring a binary tree?
The base case is when the current node is null (empty). In this case, we do nothing and return because there is no subtree to mirror.
Click to reveal answer
beginner
In C#, how do you swap the left and right children of a binary tree node?
You can swap by using a temporary variable: var temp = node.Left; node.Left = node.Right; node.Right = temp;
Click to reveal answer
intermediate
Why do we recursively call the mirror function on both left and right children?
Because each subtree must also be mirrored to create the full mirror image of the entire tree.
Click to reveal answer
intermediate
What is the time complexity of mirroring a binary tree with n nodes?
The time complexity is O(n) because each node is visited exactly once to swap its children.
Click to reveal answer
What happens to the left and right children of a node when mirroring a binary tree?
AThey are duplicated
BThey are deleted
CThey are swapped
DThey remain the same
✗ Incorrect
Mirroring swaps the left and right children of every node.
What is the base case for the recursive mirror function?
ANode is null
BNode has no children
CNode is a leaf
DNode has two children
✗ Incorrect
When the node is null, recursion stops because there is nothing to mirror.
Which traversal order is commonly used to mirror a binary tree recursively?
AInorder (left, node, right)
BPreorder (process node, then children)
CPostorder (children, then node)
DLevel order
✗ Incorrect
Preorder traversal is used because we swap children at the current node before recursively mirroring subtrees.
What is the time complexity of mirroring a binary tree with n nodes?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Each node is visited once, so the time complexity is linear, O(n).
What happens if you call the mirror function on a null node?
AIt creates a new node
BIt throws an error
CIt swaps children
DNothing, it returns immediately
✗ Incorrect
The function returns immediately on null nodes as the base case.
Explain step-by-step how to mirror a binary tree using recursion.
Think about what happens at each node and how recursion helps.
You got /4 concepts.
Describe the changes in the tree structure after mirroring a binary tree.
Imagine folding the tree along its root vertically.
You got /3 concepts.
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
Step 1: Understand the mirror operation
Mirroring means swapping left and right children at every node in the tree.
Step 2: Identify the effect on the tree
This operation flips the tree horizontally, creating a mirror image.
Final Answer:
Swapping the left and right children of every node -> Option A
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
Step 1: Understand the purpose of the function
The function modifies the tree in place and does not need to return a value.
Step 2: Choose the correct return type
Since it changes the tree nodes directly, the return type should be void.
Final Answer:
void MirrorTree(Node root) -> Option A
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
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
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]
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
Step 1: Analyze the base case
The code lacks an if (root == null) return; check before recursing on children.
Step 2: Understand why this causes error
Recursing on null children leads to NullReferenceException or stack overflow since there's no termination condition.
Final Answer:
Missing base case for null root -> Option B
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
Step 1: Consider nodes with single child
Nodes with only one child still need swapping; swapping null with a child is valid.
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.
Final Answer:
Recursively swap left and right children if node is not null, then recurse on both children -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.