Invert Binary Tree
Invert the left subtree, invert the right subtree, then put the results into swapped slots. Each subtree is the same kind of problem, so the recursion handles every level. The swap happens after both children are finished, so no pointer is read after it has been overwritten.
The tree is inverted when every node has its two children swapped, and each call makes that true for its own node after its subtrees are already done.
Problem
You are given the root of a binary tree. Flip the whole tree like a mirror: at every node, its left child and its right child swap places.
Return the root of the flipped tree. The root itself stays the same node. Only the children move.
The root of a binary tree. The tree can be empty.
The same root, after every node's two children have been swapped.
Node 4 swaps its children, so 7 comes first. Node 7 swaps 6 and 9, and node 2 swaps 1 and 3. Leaves have nothing to swap.
Only the root has children, so 1 and 3 swap.
An empty tree stays empty.
- β’ 0 β€ number of nodes β€ 100
- β’ β100 β€ Node.val β€ 100
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
If I only swap the children of the root, what is still wrong with the tree?
- 2.
Does swapping at a node also change the shape of the subtrees below it?
- 3.
Can I finish the children first and then swap at the parent? Does the order I visit nodes matter?
- 4.
If I use a queue, which nodes must I reach, and in what order?
- 5.
What should happen when a node has one child, or no children at all?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Walk the tree with BFS and swap each node's children when it is popped | O(N) | O(W) |
| Optimal | Invert the left and right subtrees first, then put them into swapped slots | O(N) | O(H) |
Why does this belong to this pattern?
- βswap left and right at every nodeβEvery node gets the same small local change, so one recursive rule can cover the whole tree.
- βreturn the rootβThe root stays the same node. Only its children move, so the answer is the tree we started with.
- βbinary treeβEach child is the root of a smaller tree, so the recursion can treat each subtree as a smaller problem.
This is the anchor of the transform sub-pattern. The change at each node is one swap, so the only thing that matters is when you read the pointers. The later problems in this group (flatten, build, serialize) also use a tree DFS, but they relink pointers, so the visit order matters even more.
Don't make this mistake
The first line overwrites root->left, so the second line inverts the wrong node. On [4,2,7,1,3,6,9], the first line puts node 7 on the left, which drops node 2 and its children. The second line inverts that same node 7 again and writes it to the right. Node 7 is now shared by both sides, and the output is [4,7,7,9,9,9,9] instead of [4,7,2,9,6,3,1].
The LeetCode input looks like a list, so it is tempting to reverse it. That does not invert the tree. For [4,2,7,1,3,6,9] the reversed list is [9,6,3,1,7,2,4], which is not the answer. Inverting moves each node's children, not the positions in the list.
A leaf has two null children. The call on a null child then reads root->left on a null pointer, so the program crashes on the first leaf. Check for null before you touch the node.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.