# 257.Binary Tree Paths

**257.Binary Tree Paths**

难度:Easy

> 给定一个二叉树，返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

```
示例:

输入:

   1
 /   \
2     3
 \
  5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
```

方法：递归求解，直到叶子节点停止。

```
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    public:
    vector<string> binaryTreePaths(TreeNode* root) {
        class ALL_PATH {
            public:
            void path(TreeNode* root, string s, vector<string> &res)
            {
                if(!root->left && !root->right) 
                    res.push_back(s+to_string(root->val));
                if(root->left)
                    path(root->left, s+to_string(root->val)+"->", res);
                if(root->right)
                    path(root->right,s+to_string(root->val)+"->", res);
            }
        };
        vector<string> res;
        if(root)
        {
             ALL_PATH ap;
            ap.path(root,"",res);
        }     
        return res;        
        
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/257.binary_tree_paths.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
