File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments