Back to problems Solve on LeetCode → ◆ Tree Patterns See #100 Same Tree →

Invert Binary Tree

LeetCode 226 • Easy • Trees

Input: root = [4, 2, 7, 1, 3, 6, 9]  →  Output: [4, 7, 2, 9, 6, 3, 1]
Swap the left and right children of every node. Mirror the tree.

TimeO(n)visit each node once
SpaceO(n)stack holds nodes
Swapped: 0/7 Stack: 0
Current
In Stack
Swapped
Stack
empty
Ready
Press Play. Pop node, swap left & right, push children. Stack-based DFS mirrors the recursive solution.