forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
36 lines (35 loc) · 811 Bytes
/
solution.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
33
34
35
36
/**
* 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:
int dfs(TreeNode *root, vector<string> &result, string temp)
{
temp += to_string(root->val);
if(!root->right && !root->left)
{
result.push_back(temp);
return 0;
}
if(root->left)
dfs(root->left,result,temp+"->");
if(root->right)
dfs(root->right,result,temp+"->");
return 0;
}
vector<string> binaryTreePaths(TreeNode* root)
{
vector<string> result;
string temp;
if(root)
dfs(root,result,temp);
return result;
}
};