LeetCode 226 Invert Binary Tree - Solution

The core idea

Recursively swap the left and right children of every node.

For each node:

  1. Recursively invert the left subtree.
  2. Recursively invert the right subtree.
  3. Swap the left and right children.

The base case: if the node is null, return null.

You can also do this iteratively using BFS or DFS with a stack, visiting each node and swapping its children.