Given a binary tree, explain how you would invert it (mirror it). Every left child becomes the right child and vice versa, at every level.
For example:
Input: Output:
4 4
/ \ / \
2 7 7 2
/ \ / \ / \ / \
1 3 6 9 9 6 3 1How do you ensure every node in the tree gets swapped? What are the edge cases?
Explain your approach and analyze the time and space complexity.
How to approach it
- Hint 1
At each node, you need to swap its left and right children.
- Hint 2
After swapping the children of the current node, you need to recursively invert the left and right subtrees.
- Hint 3
Base case: if the node is null, return null. A single-node tree is already its own mirror.
Ready to answer it out loud?
Record your answer in 1:00 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.