LC 226
LeetCode 226 · Easy

Invert Binary Tree

Invert a binary tree (swap every left and right child).


Try it

Step through the core mechanic. The simulator below runs the recursion shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is Recursion — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

Recurse: swap the children, then invert each subtree. Base case is a null node. Three lines once you trust the recursion.

AspectValue
PatternRecursion
Recognise it byMirror a tree left-to-right.
Time complexityO(n)
Space complexityO(h)
DifficultyEasy

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.