---
description: '# Easy'
---

# 226. Invert Binary Tree

{% hint style="info" %}
Be carefule with edge cases. Most of tree problems have two edge cases.

1. root = None
2. root.left = None or root.right = None
3. both of left and right are None. \[optional]
{% endhint %}

{% tabs %}
{% tab title="Python" %}
```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        # edge case
        if root == None:
            return None
        if root.left == None and root.right == None:
            return root
        
        # regular case
        temp = TreeNode(0)
        left = self.invertTree(root.left)
        right = self.invertTree(root.right)
        temp = left
        left = right
        right = temp
        
        root.left = left
        root.right = right
        
        return root
        
```
{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}

