---
# System prepended metadata

title: 257. Binary Tree Paths

---

# 257. Binary Tree Paths

https://leetcode.com/problems/binary-tree-paths/

## 思路
1. DFS基本 
2. 


```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 binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        res = []
        
        def dfs(root,s):
            
            if not root.left and not root.right:
                s = s + str(root.val)
                res.append(s)
                return 
                
            s = s+ str(root.val)+"->"    
            
            if root.left:
                left = dfs(root.left,s)
            
            if root.right:
                right = dfs(root.right,s)
            
            
        dfs(root,"")
        
        return res
        
        
        
```