Invert Binary Tree
Invert Binary Tree is LeetCode 226 (Easy). You get the root of a binary tree and must turn it into its mirror image: at every node, the left child and the right child trade places. Return the root of the mirrored tree.
- The tree has at most 100 nodes and may be empty.
- Change the tree in place; no new nodes are needed.
- The number of nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Intuition
Mirroring a whole tree sounds like a big job, but it is one small move repeated at every node. Invert binary tree comes down to this: swap the two children of each node.
Look at the root first. After its swap, the old right subtree sits on the left and the old left subtree on the right. Each of those subtrees still has its own left and right the old way round, so each needs mirroring too, which is the same problem on a smaller tree. That is why recursion fits: swap here, then invert both children.
Every node is swapped exactly once, so the work is O(n).
"Do the same thing to every node" is the shape of this problem: handle the node, then recurse into both children. Symmetric Tree (101) asks whether a tree equals its own mirror, and Same Tree (100) walks two trees side by side the same way. The invert binary tree LeetCode problem is often the first tree recursion people write.
Approach
Before reading on, mirror [4,2,7,1,3,6,9] on paper. Then decide: does it matter whether a node's children are swapped before or after the subtrees below them are inverted?
Two ways to solve it
Swap the current node's children, then call the same function on each child.
- Space: one call per level of the tree.
- Code: four short lines.
- Limit: a very deep chain could hit the recursion limit.
The standard answer for LeetCode 226.
Take nodes off a queue level by level, swap each one's children, and queue the children.
- Space: the queue holds a whole level, up to about n / 2 nodes.
- Depth: no recursion, so no stack limit.
- Order: top to bottom, one level at a time.
Handy when the tree may be very deep.
Both swap every node once in O(n) time, but on a balanced tree the recursion keeps only one path in memory, and its code is shorter. The steps, code and live demo below follow the recursive version; the queue code comes after the demo.
Stop at an empty node
If root is empty, return it as it is. An empty tree is already its own mirror. This base case ends every branch of the recursion, so a leaf needs no special code: its two missing children are just two calls that return at once.
Swap the two children
Exchange root.left and root.right in one move. In the invert binary tree Python code that is a tuple assignment; C++ uses swap and Java a temporary variable. Both pointers are read before either is overwritten, so neither subtree is lost.
Invert both subtrees, then return
Call the function on root.left and on root.right. They now hold each other's old subtrees, and each call mirrors its own subtree down to the leaves. Finally return root: the tree was changed in place, so the root is the same node.
Invert Binary Tree solution in Python | C++ | Java
Iterative BFS (queue)
The queue starts with the root. Each node taken off the queue has its two children swapped, and the children that exist join the queue, so every node is swapped once, level by level.
Common pitfalls
Assigning the children one line at a time
root.left = self.invertTree(root.right) root.right = self.invertTree(root.left)
root.left, root.right = root.right, root.left self.invertTree(root.left) self.invertTree(root.right)
The first line overwrites root.left before the second line reads it. The second call then inverts the old right subtree a second time and puts it on both sides, and the old left subtree is lost.
Forgetting to return the root
self.invertTree(root.left) self.invertTree(root.right)
self.invertTree(root.left) self.invertTree(root.right) return root
The swaps happen in place, but LeetCode reads the tree from the returned root. Without return root, Python returns None and the answer is an empty tree.
Complexity
Invert Binary Tree FAQ
Can the swap go after the recursive calls?
Yes. Swapping first or swapping last both work, because each node's children are swapped exactly once. Swapping between the two calls does not: after invert(left) and the swap, root.right is the subtree that was just inverted, so it gets inverted twice and the other one never.