-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path257_Binary_Tree_Paths.cc
More file actions
32 lines (31 loc) · 915 Bytes
/
257_Binary_Tree_Paths.cc
File metadata and controls
32 lines (31 loc) · 915 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
/**
* 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;
if(root==NULL) return result;
string curList="";
curList += to_string(root->val);
getList(root,curList,result);
return result;
}
private:
void getList(TreeNode* root,string curList,vector<string> &result){
if(root->left==NULL&&root->right==NULL){
result.push_back(curList);
return;
}
else{
if(root->left) getList(root->left,curList+"->"+to_string(root->left->val),result);
if(root->right) getList(root->right,curList+"->"+to_string(root->right->val),result);
}
}
};