LeetCode Β· #226

Invert Binary Tree Solution

Given the root of a binary tree, invert it β€” swap every left and right child β€” and return the root.

01
Section One Β· Problem

Problem Statement

🏷️

Difficulty

Easy

πŸ”—

LeetCode

Problem #226

πŸ—οΈ

Pattern

Recursion β€” post-order swap

Given the root of a binary tree, invert the tree (mirror it) and return its root. Every left child becomes the right child and vice versa, at every level.

Example
Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1
Constraints
β€’ 0 ≀ number of nodes ≀ 100 β€’ -100 ≀ Node.val ≀ 100
02
Section Two Β· Approach 1

Iterative BFS β€” O(n)

Use a queue. For each node, swap its left and right children, then enqueue both. Level-by-level inversion.

MetricValue
TimeO(n)
SpaceO(n) β€” queue holds up to n/2 nodes
03
Section Three Β· Approach 2

Recursive DFS β€” O(n)

At each node: swap left and right, then recursively invert both subtrees. Base case: null node returns null. The recursion naturally handles every level.

πŸ’‘ Mental model: Imagine mirroring a family tree β€” at every person, swap their left and right children. Do this top-down (or bottom-up β€” order doesn't matter because the swap is independent at each node).
Algorithm
  • Base case: If root is null, return null.
  • Swap: temp = root.left; root.left = root.right; root.right = temp;
  • Recurse: invertTree(root.left); invertTree(root.right);
  • Return: root.
🎯 Pattern recognition:
  • "Transform every node" = visit all nodes + do constant work each. This is the simplest tree recursion pattern.
  • It appears in LC 226, LC 101 (Symmetric Tree as a variation), and any problem that modifies tree structure.
04
Section Four Β· Trace

Visual Walkthrough

Recursive inversion β€” swap at each node
BEFORE 4 2 7 1 3 6 9 β†’ AFTER (INVERTED) 4 7 2 9 6 3 1 RECURSION TRACE β‘  Node 4: swap children 2 ↔ 7 β†’ now left=7, right=2 β‘‘ Node 7 (now left): swap children 6 ↔ 9 β†’ now left=9, right=6 β‘’ Node 2 (now right): swap children 1 ↔ 3 β†’ now left=3, right=1
05
Section Five Β· Implementation

Code β€” Java & Python

Java β€” Recursive
class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) return null; TreeNode temp = root.left; root.left = root.right; root.right = temp; invertTree(root.left); invertTree(root.right); return root; } }
Python β€” Recursive
class Solution: def invertTree(self, root): if not root: return None root.left, root.right = root.right, root.left self.invertTree(root.left) self.invertTree(root.right) return root
06
Section Six Β· Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Recursive DFS O(n) O(h) Call stack = tree height. O(log n) balanced, O(n) skewed.
Iterative BFSO(n)O(n)Queue up to n/2 nodes at widest level.
07
Section Seven Β· Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Empty treenullBase case β†’ return null.
Single node[1]Swap null↔null β†’ unchanged.
Skewed (linked list)1β†’2β†’3Swaps at each level flip left↔right. Stack depth = n.
⚠ Common Mistake: Inverting only the first level (swapping root's children but not recursing). The swap must happen at every node, not just the root.

← Back to Trees problems