Graph Theory37 sections · 1633 units
Open in Course

LeetCode 226 Invert Binary Tree - Strategy

Swap children recursively

To invert a tree, you swap the left and right children of the root. Then you invert the left subtree and the right subtree. The recursion does the rest. The base case is a null node. If the node is null, there is nothing to invert, so you return null.

If the node exists, swap its children, recurse on both, and return the node. This is tree modification in action. You are not only reading values. You are changing the structure of the tree itself.