---
# System prepended metadata

title: 'LeetCode #257 [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)'
tags: [Recursion, LeetCode, Easy, Tree]

---

###### tags: `LeetCode` `Tree` `Recursion` `Easy`
# LeetCode #257 [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)
### (Easy)
給你一個二元樹的根節點 root ，按 任意順序 ，返回所有從根節點到葉子節點的路徑。

葉子節點 是指沒有子節點的節點。


---
簡單題, 不過是結合字串的變化版。
當跳到的節點是葉節點時回傳整個字串即可。


---
```
class Solution {
public:
    vector<string> ans;
    vector<string> binaryTreePaths(TreeNode* root) {
        if(!root)return {};
        string s = to_string(root->val);
        if(!root->left&&!root->right)ans.push_back(s);
        helper(root->left, s, ans);
        helper(root->right, s, ans);
        return ans;
    }
    void helper(TreeNode* node, string s, vector<string>& ans){
        if(!node){
            return;
        }
        s+="->"+to_string(node->val);
        if(!node->left&&!node->right)ans.push_back(s);
        if(node->left)helper(node->left, s, ans);
        if(node->right)helper(node->right, s, ans);
    }
};
```