-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path257.cpp
More file actions
38 lines (35 loc) · 997 Bytes
/
Copy path257.cpp
File metadata and controls
38 lines (35 loc) · 997 Bytes
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
37
38
/**
* 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:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
dfs(root, NULL, "", result);
return result;
}
void dfs(TreeNode* current, TreeNode* prev, string tmp, vector<string>& result){
cout << tmp << endl;
if(current == NULL){
return;
}
if(prev != NULL){
tmp += "->";
}
tmp += to_string(current->val);
if(current->left == NULL && current->right == NULL){
result.push_back(tmp);
}else{
if(current->left!=NULL)
dfs(current->left, current, tmp, result);
if(current->right!=NULL)
dfs(current->right, current, tmp, result);
}
}
};