226. Invert Binary Tree
Asked at Google, Meta, Amazon, Microsoft and 1 more
Difficulty
Easy
Time
O(n)
Space
O(h)
Pattern
Depth-First Search
Companies
5
Problem
You are given the root of a binary tree. Invert the tree and return its root.
Inverting means mirroring the tree: at every node, the left child and the right child trade places. Do this for the whole tree, not just the top node.
A node may have zero, one or two children. The tree can also be empty, in which case you return nothing (null).
Mirror the whole tree
Constraints
- ●
The number of nodes in the tree is in the range [0, 100]. - ●
-100 <= Node.val <= 100
Examples
root = [4,2,7,1,3,6,9][4,7,2,9,6,3,1]root = [2,1,3][2,3,1]root = [][]Asked At
5 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → | |
| Meta | Easy | View all Meta questions → |
| Amazon | Easy | View all Amazon questions → |
| Microsoft | Easy | View all Microsoft questions → |
| Apple | Easy | View all Apple questions → |
How to Think About It
Recursive approach: swap left and right children, then recurse on both subtrees. The base case is a null node.
Why it works: by swapping at every node, the entire tree becomes its mirror image. Left becomes right, right becomes left.
BFS alternative: use a queue, swap children at each node during level-order traversal. Same result, different traversal order.
Edge cases: empty tree (return null), single node (return as-is), already inverted (same result).
Optimal Approach
Recursive DFS:
Step 1: Base case — if root is null, return null.
Step 2: Swap root.left and root.right.
Step 3: Recursively invert root.left and root.right.
Step 4: Return root.
The swap happens before recursion, so the entire subtree is inverted as we unwind.
Time: O(n) — visit every node once. Space: O(h) — recursion stack.
Swap first, then recurse
What Trips People Up in Real Interviews
Not handling the base case. If the node is null, return null. Without this, you'll get a null pointer exception.
Forgetting to recurse after swapping. You need to swap children AND then invert both subtrees.
Confusing "invert" with "mirror image." Inverting a binary tree means every left child becomes right and vice versa. The result is a mirror image.
Not handling a single node. A single node tree is already inverted — just return it.
Trying to invert by traversing and building a new tree. The problem says to invert in-place by swapping children. Don't create new nodes — just swap existing left and right pointers.
Solution Code
O(n) timedef invertTree(root):
if not root:
return None
root.left, root.right = root.right, root.left
invertTree(root.left)
invertTree(root.right)
return rootFrequently Asked Questions
What is the Invert Binary Tree problem?
Given the root of a binary tree, invert the tree and return its root. Inverting means swapping every left and right child recursively. Made famous by the "would you invert a binary tree?" meme.
How do you solve Invert Binary Tree?
The optimal approach runs in O(n) time and O(h) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Invert Binary Tree?
Invert Binary Tree is asked at Google, Meta, Amazon, Microsoft, Apple. It is a easy difficulty problem.
What are common mistakes on Invert Binary Tree?
- Not handling the base case. If the node is `null`, return `null`. Without this, you'll get a `null` pointer exception.
- Forgetting to recurse after swapping. You need to swap children AND then invert both subtrees.
- Confusing "invert" with "mirror image." Inverting a binary tree means every left child becomes right and vice versa. The result is a mirror image.
- Not handling a single node. A single node tree is already inverted — just return it.
- Trying to invert by traversing and building a new tree. The problem says to invert in-place by swapping children. Don't create new nodes — just swap existing left and right pointers.