> For the complete documentation index, see [llms.txt](https://liuxue2010.gitbook.io/data-structure-and-algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuxue2010.gitbook.io/data-structure-and-algorithms/binary-tree-and-divide-conquer/binary-tree-paths.md).

# Binary Tree Paths\*

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

```
   1
 /   \
2     3
 \
  5
```

All root-to-leaf paths are:

```
["1->2->5", "1->3"]
```

Solution:

这是简单的DFS的遍历题目，由于需要不停的传递进入str变量，这个时候有两种做法，一种就是默认根节点有值，然后-> 指向子节点，另外一种就是我地下的做法。

```
class Solution(object):
    def binaryTreePaths(self, root):
        if root == None:
            return []
        result = []
        self.dfs(root, result, '')
        return result
    def dfs(self, root, result, strs):
        if root == None:
            return
        s = s + '->' + str(root.val) if s != '' else str(root.val)
        if root.left == None and root.right == None:
            result.append(strs)
        if root.left:
            self.dfs(root.left, result, strs)
        if root.right:
            self.dfs(root.right, result, strs)
```
