AlgoMaster Logo

Invert Binary Tree

Last Updated: April 1, 2026

easy
4 min read

Understanding the Problem

We need to mirror a binary tree around its vertical axis. Every node's left child becomes its right child, and vice versa. This swap has to happen at every single node, not just the root.

A common mistake is thinking you only need to swap the root's children. But inversion is recursive by nature. When you swap the left and right subtrees of the root, the subtrees themselves also need to be inverted internally. The node that was in the bottom-left corner of the original tree should end up in the bottom-right corner of the inverted tree.

The key observation is that the structure of the problem is naturally recursive. To invert a tree rooted at some node, you invert the left subtree, invert the right subtree, then swap the two. This maps directly to a simple recursive algorithm.

Key Constraints:

  • Number of nodes in [0, 100] -- With n up to 100, any reasonable algorithm works. But since we need to visit every node at least once, O(n) is the natural target.
  • -100 <= Node.val <= 100 -- Node values are irrelevant to the algorithm. We are rearranging tree structure, not processing values.
  • 0 nodes is valid -- We need to handle the empty tree (null root) case.

Approach 1: DFS Recursive

Intuition

The most natural way to think about this problem is recursively. To invert a tree rooted at some node, we need to do three things: invert the left subtree, invert the right subtree, and then swap the left and right children of the current node.

The base case is simple. If the node is null, there's nothing to invert, so we return null.

This maps directly to a post-order DFS traversal. We go deep into the left subtree first, invert everything there, then go deep into the right subtree and invert everything there, and finally swap the two children at the current node. The beauty of recursion is that by the time we do the swap at any node, both subtrees are already fully inverted.

Actually, the order of operations is flexible here. You can swap first and then recurse (pre-order), or recurse first and then swap (post-order). Both work because swapping at a node is independent of whether the subtrees have been inverted yet. The only thing that matters is that every node gets visited and its children get swapped.

Algorithm

  1. If the root is null, return null.
  2. Recursively invert the left subtree.
  3. Recursively invert the right subtree.
  4. Swap the left and right children of the current node.
  5. Return the current node.

Example Walkthrough

1Start: call invertTree(4), recurse into left subtree first
4root213769
1/7

Code

The recursive approach is clean and optimal in time, but it uses the implicit call stack. Can we achieve the same result iteratively using BFS?

Approach 2: BFS Iterative (Level-Order)

Intuition

Instead of using recursion, we can use an explicit queue and process the tree level by level. The idea is simple: use BFS to visit every node, and at each node, swap its left and right children.

This works because the inversion at any node is independent of the order in which we visit nodes. Whether we visit top-down, bottom-up, left-to-right, or level-by-level, as long as we swap the children at every node, the tree gets fully inverted. BFS just happens to be a clean, iterative way to guarantee we visit every node.

Algorithm

  1. If the root is null, return null.
  2. Create a queue and add the root to it.
  3. While the queue is not empty: dequeue a node, swap its left and right children, and enqueue any non-null children.
  4. Return the root.

Example Walkthrough

1Initialize queue with root. Queue: [4]
4dequeue213769
1/6
1Queue initialized with root node 4
Front
4
Rear
1/6

Code

The BFS approach avoids recursion but can use O(n) space for wide trees. Can we get iterative DFS with O(h) space instead?

Approach 3: DFS Iterative (Using Stack)

Intuition

This approach combines the best of both worlds: iterative (no risk of call stack overflow) and DFS-style space usage of O(h). Instead of a queue (BFS), we use a stack (DFS). The logic at each node is identical: swap the children.

The stack gives us depth-first traversal order. We push a node, pop it, swap its children, then push the non-null children onto the stack. The last-in-first-out nature of the stack means we go deep before going wide, which keeps the stack size bounded by the tree height rather than the tree width.

Algorithm

  1. If the root is null, return null.
  2. Create a stack and push the root onto it.
  3. While the stack is not empty: pop a node, swap its left and right children, and push any non-null children onto the stack.
  4. Return the root.

Example Walkthrough

1Start: call invertTree(4), recurse into left subtree first
4root213769
1/7

Code