|
| 1 | +# Invert Binary Tree |
| 2 | + |
| 3 | +**Difficulty**: Easy |
| 4 | +**Category**: Trees, Recursion, Binary Tree |
| 5 | +**Leetcode Link**: [https://leetcode.com/problems/invert-binary-tree/](https://leetcode.com/problems/invert-binary-tree/) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 📝 Introduction |
| 10 | + |
| 11 | +The **Invert Binary Tree** problem asks us to take a binary tree and **invert** it. |
| 12 | + |
| 13 | +Given the `root` of a binary tree, our goal is to return the **root of the inverted tree**. |
| 14 | + |
| 15 | +**Expected Output**: Return the root of the new inverted tree (mirror image of the original). |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 💡 Approach & Key Insights |
| 20 | + |
| 21 | +The key insight is that at each node in the binary tree, we need to **swap** the left and right child. We can do this using: |
| 22 | +- **Recursion** (Depth-First Traversal) |
| 23 | +- **Iteration** using a Queue (Breadth-First Traversal) |
| 24 | + |
| 25 | +It's essentially a tree traversal problem and the recursive solution felt like the intuitive approach. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 🛠️ Breakdown of Approaches |
| 30 | + |
| 31 | +### 1️⃣ Brute Force / Naive Approach |
| 32 | + |
| 33 | +#### Explanation: |
| 34 | +- Traverse the tree using any traversal (preorder/inorder/postorder). |
| 35 | +- For each node, store left and right subtrees separately. |
| 36 | +- Reconstruct the tree manually by reassigning children in inverted order. |
| 37 | +- This is impractical and complex for a simple task. |
| 38 | + |
| 39 | +#### Time Complexity: |
| 40 | +`O(n)` – We still visit every node once. |
| 41 | + |
| 42 | +#### Space Complexity: |
| 43 | +`O(n)` – We use space to store and reconstruct the tree. |
| 44 | + |
| 45 | +#### Example/Dry Run: |
| 46 | + |
| 47 | +> ⚠️ Skipping detailed dry run as this approach is rarely used in practice for this problem. |
| 48 | +
|
| 49 | +--- |
| 50 | + |
| 51 | +### 2️⃣ Optimized Approach (Recursive DFS) |
| 52 | + |
| 53 | +#### Explanation: |
| 54 | +- If the root is `NULL`, return `NULL`. |
| 55 | +- Recursively invert the left and right subtrees. |
| 56 | +- Swap the left and right pointers at the current node. |
| 57 | +- Return the node. |
| 58 | + |
| 59 | +```c |
| 60 | +struct TreeNode* invertTree(struct TreeNode* root) { |
| 61 | + if (root == NULL) |
| 62 | + return NULL; |
| 63 | + |
| 64 | + struct TreeNode* temp = root->right; |
| 65 | + root->right = root->left; |
| 66 | + root->left = temp; |
| 67 | + |
| 68 | + invertTree(root->right); |
| 69 | + invertTree(root->left); |
| 70 | + |
| 71 | + return root; |
| 72 | +} |
| 73 | +``` |
| 74 | +
|
| 75 | +#### Time Complexity: |
| 76 | +`O(n)` – Every node is visited exactly once. |
| 77 | +
|
| 78 | +#### Space Complexity: |
| 79 | +`O(h)` – Due to the recursion stack. *h* is the height of the tree (O(log n) for balanced, O(n) for skewed). |
| 80 | +
|
| 81 | +#### Example/Dry Run: |
| 82 | +
|
| 83 | +Example Input (Tree): |
| 84 | +``` |
| 85 | + 1 |
| 86 | + / \ |
| 87 | + 2 3 |
| 88 | + / \ |
| 89 | +4 5 |
| 90 | +``` |
| 91 | +
|
| 92 | +Step-by-step: |
| 93 | +- At node 1: swap 2 and 3. |
| 94 | +- Recurse into 3 and 2. |
| 95 | +- At node 2: swap 4 and 5. |
| 96 | +- Final inverted tree: |
| 97 | +``` |
| 98 | + 1 |
| 99 | + / \ |
| 100 | + 3 2 |
| 101 | + / \ |
| 102 | + 5 4 |
| 103 | +``` |
| 104 | +
|
| 105 | +--- |
| 106 | +
|
| 107 | +### 3️⃣ Best / Final Optimized Approach (Iterative BFS) |
| 108 | +
|
| 109 | +#### Explanation: |
| 110 | +- Use a queue to traverse the tree level by level. |
| 111 | +- At each node, swap the left and right children. |
| 112 | +- Enqueue non-null children for further processing. |
| 113 | +
|
| 114 | +```c |
| 115 | +struct TreeNode* invertTree(struct TreeNode* root) { |
| 116 | + if (root == NULL) |
| 117 | + return NULL; |
| 118 | +
|
| 119 | + struct TreeNode* queue[100]; |
| 120 | + int front = 0, rear = 0; |
| 121 | + queue[rear++] = root; |
| 122 | +
|
| 123 | + while (front < rear) { |
| 124 | + struct TreeNode* node = queue[front++]; |
| 125 | +
|
| 126 | + struct TreeNode* temp = node->left; |
| 127 | + node->left = node->right; |
| 128 | + node->right = temp; |
| 129 | +
|
| 130 | + if (node->left) queue[rear++] = node->left; |
| 131 | + if (node->right) queue[rear++] = node->right; |
| 132 | + } |
| 133 | +
|
| 134 | + return root; |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +#### Time Complexity: |
| 139 | +`O(n)` – Each node is visited once. |
| 140 | + |
| 141 | +#### Space Complexity: |
| 142 | +`O(n)` – In the worst case, all nodes may be in the queue (for a full binary tree). |
| 143 | + |
| 144 | +#### Example/Dry Run: |
| 145 | + |
| 146 | +Input Tree: |
| 147 | +``` |
| 148 | + 1 |
| 149 | + / \ |
| 150 | + 2 3 |
| 151 | +``` |
| 152 | + |
| 153 | +- Queue: [1] |
| 154 | +- Process 1 → Swap 2 & 3 |
| 155 | +- Queue: [3, 2] |
| 156 | +- Process 3 → Swap NULLs |
| 157 | +- Process 2 → Swap NULLs |
| 158 | + |
| 159 | +Final Tree: |
| 160 | +``` |
| 161 | + 1 |
| 162 | + / \ |
| 163 | + 3 2 |
| 164 | +``` |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## 📊 Complexity Analysis |
| 169 | + |
| 170 | +| Approach | Time Complexity | Space Complexity | |
| 171 | +|------------------|------------------|-------------------| |
| 172 | +| Brute Force | O(n) | O(n) | |
| 173 | +| Optimized (DFS) | O(n) | O(h) | |
| 174 | +| Best (BFS) | O(n) | O(n) | |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +## 📉 Optimization Ideas |
| 179 | + |
| 180 | +- For deep trees where recursion depth could cause stack overflow, use **iterative BFS**. |
| 181 | +- If modifying the original tree is not allowed, we could clone and then invert, but that increases space usage. |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## 📌 Example Walkthroughs & Dry Runs |
| 186 | + |
| 187 | +### Input Tree: |
| 188 | +``` |
| 189 | + 4 |
| 190 | + / \ |
| 191 | + 2 7 |
| 192 | + / \/ \ |
| 193 | +1 3 6 9 |
| 194 | +``` |
| 195 | + |
| 196 | +### Process: |
| 197 | +- Swap 2 & 7 → Recurse |
| 198 | +- Swap 1 & 3 in subtree of 2 |
| 199 | +- Swap 6 & 9 in subtree of 7 |
| 200 | + |
| 201 | +### Output Tree: |
| 202 | +``` |
| 203 | + 4 |
| 204 | + / \ |
| 205 | + 7 2 |
| 206 | + / \/ \ |
| 207 | +9 6 3 1 |
| 208 | +``` |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +## 🔗 Additional Resources |
| 213 | + |
| 214 | +- [YouTube: NeetCode - Invert Binary Tree](https://www.youtube.com/watch?v=OnSn2XEQ4MY) |
| 215 | +- [Binary Tree Traversal Explanation](https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/) |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +**Author**: hanzel-sc |
| 220 | +**Date**: 25/07/2025 |
0 commit comments