Invert Binary Tree
Swap the left and right child of every tree node.
Why does this pattern fit?
Restate the exact job
Swap the left and right child of every tree node.
Each node’s answer depends only on inverted child subtrees.
O(n) time · O(h) call stack
Swapping after overwriting one child can lose a subtree.
How to solve Invert Binary Tree
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Swap the left and right child of every tree node.
Why Trees & recursive traversal fits
Each node’s answer depends only on inverted child subtrees.
State to maintain
A node and the inverted results of its children.
Transition
Recursively invert both children, swap them, and return the node.
Time and space
O(n) time · O(h) call stack
Counterexample to the tempting mistake
Swapping after overwriting one child can lose a subtree.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.