> For the complete documentation index, see [llms.txt](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/tree/257.-binary-tree-paths.md).

# 257. Binary Tree Paths

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

**Note:** A leaf is a node with no children.

**Example:**

```
Input:

   1
 /   \
2     3
 \
  5

Output: ["1->2->5", "1->3"]

Explanation: All root-to-leaf paths are: 1->2->5, 1->3
```

```cpp
void helper(TreeNode* node, string cur, vector<string>& res) {
    if (!node->left && !node->right) {
        cur += to_string(node->val);
        res.push_back(cur);
        return;
    }
    cur += to_string(node->val) + "->";
    if (node->left) helper(node->left, cur, res);
    if (node->right) helper(node->right, cur, res);
}
vector<string> binaryTreePaths(TreeNode* root) {
    vector<string> res;
    if (!root) return res;
    string cur;
    helper(root, cur, res);
    return res;
}
```

```cpp
void helper(TreeNode* node, string cur, vector<string>& res) {
    if (!node->left && !node->right) {
        res.push_back(cur);
        return;
    }
    if (node->left) helper(node->left, cur + "->" + to_string(node->left->val), res);
    if (node->right) helper(node->right, cur + "->" + to_string(node->right->val), res);
}
vector<string> binaryTreePaths(TreeNode* root) {
    vector<string> res;
    if (!root) return res;
    string cur = to_string(root->val);
    helper(root, cur, res);
    return res;
}
```
