---
# System prepended metadata

title: 226. Invert Binary Tree

---

# 226. Invert Binary Tree

## 思路
1. 二元樹tree 的基本方式
2. root 是recursive 最上層的，所以root 會是樹根，不是最底的子樹


```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: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root :
            return 
        
        root.left,root.right=root.right,root.left 
        
        self.invertTree(root.left )
        self.invertTree(root.right)
        
        
        return root 
       
  
                
        
        
```
