Invert Binary Tree
You are given the root of a binary tree. Turn it into its mirror image: for every node, its left and right subtrees trade places. Return the root of the resulting tree.
After the change, reading any level of the tree from left to right gives the values of that level in the original tree from right to left.
Trees are written in level order: the root first, then each level from left to right, with null marking a missing child. For example [4,2,null,1] is a root 4 whose left child 2 has a left child 1.
Example 1
Example 2
Example 3
Constraints
0 <= number of nodes <= 1000-1000 <= Node.val <= 1000- The depth of the tree is at most 100.
Pattern Path · Trees: DFS · problem 80 of 169
Start simple
Serialise each level, reverse it and rebuild a new tree from the reversed levels: still O(n) time, but O(n) extra memory and much more code than swapping in place.
The key idea
The mirror of a tree has a neat recursive description: the mirrored tree has the same root, its left subtree is the mirror of the original right subtree, and its right subtree is the mirror of the original left subtree. An empty tree is its own mirror, which is the base case.
That definition translates directly into code: mirror both children, then assign them to the opposite sides of the node. Because the work at each node is a constant-time swap and every node is visited once, the whole tree is mirrored in linear time, and it is done in place without creating new nodes.
The order does not matter: you can swap first and recurse afterwards, or traverse level by level with a queue and swap each node's children as you pop it. Every node just needs to be visited once. The recursive version uses stack space proportional to the height of the tree.
Complexity
- Time:
O(n) - Space:
O(h)
Python
class Solution:
def invertTree(self, root):
if root is None:
return None
# Mirror both subtrees, then put each one on the opposite side.
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
return root
JavaScript
var invertTree = function(root) {
if (root === null) return null;
// Mirror both subtrees, then put each one on the opposite side.
const left = invertTree(root.left);
root.left = invertTree(root.right);
root.right = left;
return root;
};
What the interviewer is checking
This is a quick check that you can think recursively about trees, state a base case, and visit every node exactly once, or do the same iteratively with a queue.
Mistakes to avoid
- Swapping only the root's children and forgetting to mirror the subtrees below them.
- Assigning root.left = invert(root.right) and then root.right = invert(root.left) in two steps, which reads the already-overwritten left child.
- Forgetting the empty-tree case and dereferencing a null root.
Follow-ups to expect
- Can you do it without recursion? (BFS with a queue, swapping each node.)
- How would you check whether a tree is symmetric? (Compare it against its own mirror.)
- How would you check if two trees are mirrors of each other? (Recursive left-right cross comparison.)
In real software
Mirroring hierarchical layouts, such as flipping a UI component tree or an org chart for right-to-left languages, applies the same swap at every node.