Invert a Binary Tree
In an inverted binary tree, each node in the original is swapped with the corresponding node on the other side.
You can also think of it as rotating the tree on an axis right down the middle.
Example:
Here’s the same tree, inverted.
Like most tree problems, it’s best handled recursively. You just
- Swap
- Recurse to the left child
- Recurse to the right child
It’s surprisingly simple.
Wow! You read the whole thing. People who make it this far sometimes
want to receive emails when I post something new.
I also have an RSS feed.