-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path257.cpp
32 lines (30 loc) · 830 Bytes
/
257.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
vector<string> ret;
vector<string> binaryTreePaths(TreeNode *root) {
ret.clear();
if (!root)
return ret;
string path;
path += to_string(root->val);
helper(root->left, path);
helper(root->right, path);
if (ret.empty())
ret.push_back(path);
return ret;
}
void helper(TreeNode *root, string &path) {
if (!root)
return;
if (!root->left && !root->right) {
string &&temp = path + "->" + to_string(root->val);
ret.push_back(temp);
return;
}
int &&size = path.size();
path += "->" + to_string(root->val);
helper(root->left, path);
helper(root->right, path);
path = path.substr(0, size);
}
};