> For the complete documentation index, see [llms.txt](https://chunhthanhde.gitbook.io/leetcode-top-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-9-binary-tree-general/070-invert-binary-tree.md).

# 226. Invert Binary Tree 🔗

**Difficulty**: `Easy` - **Tags**: `Binary Tree`, `DFS`, `BFS`, `Recursion`

[LeetCode Problem Link](https://leetcode.com/problems/invert-binary-tree/)

***

## Problem Statement 📜

Given the root of a binary tree, invert the tree (swap the left and right subtrees), and return its root.

***

## Examples 🌟

🔹 **Example 1**:

**Input**:

```plaintext
root = [4,2,7,1,3,6,9]
```

**Output**:

```plaintext
[4,7,2,9,6,3,1]
```

***

🔹 **Example 2**:

**Input**:

```plaintext
root = [2,1,3]
```

**Output**:

```plaintext
[2,3,1]
```

***

🔹 **Example 3**:

**Input**:

```plaintext
root = []
```

**Output**:

```plaintext
[]
```

***

## Constraints ⚙️

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`.

***

## Solution 💡

To invert a binary tree:

1. Swap the left and right children of the current node.
2. Recursively apply the same operation for each child.

***

### Java Solution (Recursive Approach)

```java
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null; // Base case: empty tree
        }

        // Swap the left and right subtrees
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        // Recursively invert the left and right subtrees
        invertTree(root.left);
        invertTree(root.right);

        return root; // Return the modified tree
    }
}
```

***

## Explanation of the Solution

1. **Base Case**: If the current node is `null`, return `null`.
2. Swap the left and right children of the node.
3. Recursively call `invertTree` for the left and right subtrees.
4. Return the root after performing all swaps.

***

### Java Solution (Iterative Approach with Queue)

```java
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode current = queue.poll();

            // Swap the left and right subtrees
            TreeNode temp = current.left;
            current.left = current.right;
            current.right = temp;

            // Add children to the queue
            if (current.left != null) {
                queue.offer(current.left);
            }
            if (current.right != null) {
                queue.offer(current.right);
            }
        }

        return root; // Return the modified tree
    }
}
```

***

## Time Complexity ⏳

* **O(n)**, where `n` is the number of nodes in the tree. Each node is visited once.

## Space Complexity 💾

* **O(h)** for the recursive approach, where `h` is the height of the tree (stack space for recursion).
* **O(n)** for the iterative approach due to the queue.

***

## Follow-up 🧐

* How would you modify the solution for a n-ary tree?
* Implement a solution to invert a binary tree in postorder traversal.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-9-binary-tree-general/070-invert-binary-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
