You get the root of a binary tree. Invert it: for every node, its left subtree becomes its right one and the other way round, so the whole tree turns into its mirror image. Return the root.
Example: a root 8 with 3 on the left (children 1 and 6) and 12 on the right (children 10 and 15) becomes 8 with 12 on the left (children 15 and 10) and 3 on the right (children 6 and 1).
Try it yourself first: LeetCode 226 ↗. Then step through the solution below.
Build a mirrored copy: create a new node for every node, with the copy of its right subtree on the left and the copy of its left subtree on the right. That is O(n) time too, but it allocates n new nodes, O(n) extra space, when the original nodes can simply be relinked.
The mirror of a tree is its root with the mirror of the right subtree on the left and the mirror of the left subtree on the right. So a node has only one job: swap its two children. Then the same job is left for each child.
That is the same problem on smaller trees, which is what recursion is for: swap at the root, call the function on both children, and stop at an empty child.
Note
A common slip is two plain lines: root.left = invert(root.right), then root.right = invert(root.left). The second line reads the new left child, so the old left subtree is lost. Save one child first, or swap both in one statement as here. The swap can come before the two calls or after them: both give the mirror. Between them it cannot: after the left subtree is inverted and the children are swapped, the right child is that same subtree, so a call on it inverts it back.
The player runs the code on the example. Press → for each step: the lines it runs light up in the code.
On a phone, Full screen keeps the picture and the code on one screen.
class TreeNode: def __init__(self, val, left=None, right=None): self.val, self.left, self.right = val, left, right def invert(root): if root is None: return None root.left, root.right = root.right, root.left invert(root.left) invert(root.right) return root
Now write it yourself on LeetCode 226 ↗, or go on: Binary Tree Level Order Traversal →
| Approach | Time | Space |
|---|---|---|
| Brute force: build a mirrored copy | O(n) | O(n) |
| A queue instead of recursion | O(n) | O(w) |
| Swap the children, recursively this page | O(n) | O(h) |
h is the height of the tree, and w the number of nodes on its widest level.
Note
Python allows 1,000 nested calls by default, so a chain of about a thousand nodes raises RecursionError. Java and C++ have no fixed count: the limit is the thread's stack size, which usually allows many thousands of calls.
None at once.The blocks below are steps of Invert Binary Tree in words, and one of them does not belong. A block that works on a subtree “the same way” runs your lines again, from line 1, on that subtree. Put the right ones in order and check: your algorithm runs on the example and on a chain, 3 with a left child 2 with a left child 1, so a mistake shows in its answer. Once it is right, the code appears beside your blocks.
Click the blocks in the order they run, or drag them into the lines.Tap the blocks in the order they run. One of them doesn't belong.
Your algorithm
To invert the tree under root:
Blocks
Now write it yourself on LeetCode 226 ↗, or go on: Binary Tree Level Order Traversal →
class TreeNode: def __init__(self, val, left=None, right=None): self.val, self.left, self.right = val, left, right def invert(root): if root is None: return None root.left, root.right = root.right, root.left invert(root.left) invert(root.right) return rootCan you do it without recursion? Yes, with a queue: take a node out, swap its children, and put the children that are not empty in. It is the same O(n) time, the queue holds the end of one level and the start of the next, and a very deep tree cannot hit the recursion limit.
LeetCode is a trademark of LeetCode LLC. VisiGrab is not affiliated with LeetCode or with Blind; the problems are told in our own words, with our own examples.