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 simply return because there is nothing to mirror.
Click to reveal answer
beginner
How do you swap the children of a node in a binary tree?
You temporarily store one child, assign the other child to its place, then assign the stored child to the other place. For example, swap left and right pointers.
Click to reveal answer
intermediate
Why is recursion a good approach to mirror a binary tree?
Because each node's children can be mirrored independently, recursion naturally handles the process by mirroring subtrees from bottom up.
Click to reveal answer
intermediate
What will be the output tree after mirroring this tree?
1
/ \
2 3
/ \
4 5
The mirrored tree will be:
1
/ \
3 2
/ \
5 4
Click to reveal answer
What is the first step in mirroring a binary tree node?
AAdd a new child
BDelete the node
CSwap the left and right children
DDo nothing
✗ Incorrect
Mirroring involves swapping the left and right children of each node.
What happens when the mirror function reaches a null node?
AIt swaps children
BIt returns immediately
CIt creates a new node
DIt throws an error
✗ Incorrect
The base case for recursion is when the node is null, so the function returns immediately.
Which traversal method is naturally used in mirroring a binary tree?
APreorder traversal
BLevel order traversal
CPostorder traversal
DInorder traversal
✗ Incorrect
Preorder traversal is used because we swap children before recursively mirroring subtrees.
After mirroring, what happens to the left subtree of the original tree?
AIt stays the left subtree
BIt becomes the root
CIt is deleted
DIt becomes the right subtree
✗ Incorrect
The left subtree becomes the right subtree after mirroring.
Which of these is NOT a correct step in mirroring a binary tree?
ADelete all leaf nodes
BRecursively mirror left subtree
CRecursively mirror right subtree
DSwap left and right children of the node
✗ Incorrect
Deleting leaf nodes is not part of mirroring a binary tree.
Explain how to mirror a binary tree using recursion.
Think about what happens at each node and how recursion helps.
You got /3 concepts.
Draw or describe the mirrored version of a simple binary tree with root and two children.
Visualize swapping the left and right branches.
You got /3 concepts.
Practice
(1/5)
1. What does mirroring a binary tree mean? Mirror a Binary Tree means:
easy
A. Swapping the left and right children of every node
B. Deleting all leaf nodes
C. Reversing the values of nodes in each level
D. Sorting the nodes in ascending order
Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node in the tree.
Step 2: Compare with other options
Deleting leaves, reversing values, or sorting nodes are different operations and do not create 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 reversing node values only
Confusing mirror with tree traversal
Assuming mirror deletes nodes
2. Which of the following is the correct Python function header to define a recursive mirror function for a binary tree node named root?
easy
A. def mirror(root):
B. def mirror(root) -> int:
C. def mirror(root) -> None:
D. def mirror(root) -> str:
Solution
Step 1: Identify function purpose and return type
The mirror function modifies the tree in place and returns nothing, so return type is None.
Step 2: Check syntax correctness
def mirror(root) -> None: uses correct Python syntax with type hint for no return value.
Final Answer:
def mirror(root) -> None: -> Option C
Quick Check:
Function returns nothing = None [OK]
Hint: Mirror modifies tree, returns nothing, so use -> None [OK]
Common Mistakes:
Using wrong return type like int or str
Omitting parentheses in function definition
Missing colon at end of function header
3. What will be the output of the following code after calling mirror(root)? Assume the tree is: 1 / \ 2 3