Skip to content

Commit a1cf9e1

Browse files
author
cpppy
authored
Create 257_Binary_Tree_Paths.cc
1 parent 0ff469d commit a1cf9e1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

257_Binary_Tree_Paths.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
vector<string> binaryTreePaths(TreeNode* root) {
13+
vector<string> result;
14+
if(root==NULL) return result;
15+
string curList="";
16+
curList += to_string(root->val);
17+
getList(root,curList,result);
18+
return result;
19+
20+
}
21+
private:
22+
void getList(TreeNode* root,string curList,vector<string> &result){
23+
if(root->left==NULL&&root->right==NULL){
24+
result.push_back(curList);
25+
return;
26+
}
27+
else{
28+
if(root->left) getList(root->left,curList+"->"+to_string(root->left->val),result);
29+
if(root->right) getList(root->right,curList+"->"+to_string(root->right->val),result);
30+
}
31+
}
32+
};

0 commit comments

Comments
 (0)