Easy
Dec 23, 2025#binary-tree
#bfs
226. Invert Binary Tree
Given the root of a binary tree, invert the tree, and return its root.
Example 1:
Example 2:
Example 3:
- The number of nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Constraints:
Notes
Intuition
Inverting a tree means swapping every node's left and right children. We can traverse the tree using BFS (or DFS) and perform the swap at each node.
Implementation
Handle the empty tree case first. Initialize a queue with the root. For each node dequeued, swap its left and right children. Then enqueue any non-null children to continue the traversal. Return the original root reference (now pointing to the inverted tree).
Edge-cases
An empty tree (null root) should return null immediately. The algorithm handles single-node trees naturally.
- Time: O(n) — visit each node once
- Space: O(n) — queue holds up to n/2 nodes at the widest level