---
# System prepended metadata

title: self.funciton 的self 使用時機

---

# self.funciton 的self 使用時機


大原則：在class 下呼叫自己的function 都要加self t

```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 
       
  
                
        
        
```