257. Binary Tree Paths

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<string> ret;
void traverse(TreeNode *root, string path){
if(!root) return;


if(!root->right && !root->left){
path+=to_string(root->val);
ret.push_back(path);
return;
}
else path+=to_string(root->val)+"->";
traverse(root->left,path);
traverse(root->right,path);

}
vector<string> binaryTreePaths(TreeNode* root) {
string path;
traverse(root, path);
return ret;
}
};