Problem overview
The method invertTree receives the root of a binary tree and returns the root of the same tree, mirrored from left to right. At every node the left child and the right child trade places, all the way down to the leaves. The lesson provides the TreeNode class, with a val and two children, left and right, each a TreeNode or null.
For the tree [4,2,7,1,3,6,9] the result is [4,7,2,9,6,3,1]. Under the root, 2 and 7 swap places, then 1 and 3 swap under 2, and 6 and 9 swap under 7. For [1,2,null,3], a line of nodes going left becomes a line going right: [1,null,2,null,3].
Approach
A mirror image swaps left and right at every node, and nothing else. So the work at one node is: swap its two children, then mirror each of the two subtrees the same way. Every subtree is a smaller tree of the same kind, which makes recursion a natural fit. An empty tree is its own mirror, which ends the recursion.
The tree is changed in place: the same nodes are reused and only their left and right links change. Each node is visited once, so the time is O(n).
Step by step
- Base case. If
rootisnull(!root), returnnull. This also stops the recursion when it steps past a leaf. - Swap the children. One destructuring assignment does it: the array
[root.left, root.right]on the left of the=receives the array[root.right, root.left]on its right. The array on the right is built first, from the old children, so neither one is lost. - Mirror the left subtree. Call
this.invertTree(root.left). It swaps the children all the way down that subtree, in place, so the node it returns does not need to be kept. - Mirror the right subtree. Call
this.invertTree(root.right)the same way. After both calls, every node below the root has had its children swapped too. - Return
root. It is still the top of the tree, which is now mirrored.
The solution in TypeScript
class Solution {
invertTree(root: TreeNode | null): TreeNode | null {
if (!root) {
return null;
}
[root.left, root.right] = [root.right, root.left];
this.invertTree(root.left);
this.invertTree(root.right);
return root;
}
}
Details for TypeScript:
- The destructuring swap.
[a, b] = [b, a]needs no temporary variable. The naive version fails: withroot.left = root.right; root.right = root.left;, the first line overwrites the left child, so the second line copies the right child back onto itself. For a root with children 2 and 3, that leaves both children as 3, while the destructuring swap leaves 3 on the left and 2 on the right. - The assignment targets are properties. Destructuring can assign to
root.leftandroot.rightdirectly, not only to variables. - The order of swap and calls. Swapping first and recursing after, as here, mirrors the same subtrees as recursing first and swapping after. Either order gives the same tree, as long as each node's swap happens exactly once.
- The return values of the recursive calls are ignored. The calls change the nodes themselves, so
root.leftandroot.rightalready hold the mirrored subtrees. - Types.
TreeNode | nullis erased when the code compiles to JavaScript. At run time the only protection against an empty tree is the!rootcheck.
Complexity
Time: O(n). Every node is visited once and does one swap. Space: O(h) for the call stack, where h is the height of the tree: about log n for a balanced tree and n for a chain. The lesson's trees are at most 400 levels deep, which is well within the call stack.
Edge cases and common mistakes
- Empty tree.
root = []arrives asnulland the method returnsnull: nothing to swap. - A single node. Both children are
null; swapping them changes nothing, and the node is returned as it is. - A missing child. Swapping a node with one child and one
nullmoves the child to the other side. This is how[1,2,null,3]becomes[1,null,2,null,3]. - Swapping only the root's children. That mirrors the top level but leaves the subtrees pointing the original way. The recursion is what carries the swap down.
- Swapping the values instead of the links. Exchanging the
valfields of the children would not move their subtrees. - Inverting twice. Applying
invertTreetwice gives back the original tree:[4,2,7,1,3,6,9]comes out unchanged.
Testing
The results below are written in level order, with null for a missing child and the trailing ones left out:
| Tree | Result |
|---|---|
[4,2,7,1,3,6,9] | [4,7,2,9,6,3,1] |
[1,2,null,3] | [1,null,2,null,3] |
[] | [] |
[8] | [8] |
[1,2,3] | [1,3,2] |
