257. 二叉树的所有路径

 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 {
12 public:
13     vector<string> res;
14     void findTreePaths(TreeNode* root, string s)
15     {
16         if(!root) return;
17         if(!root->left && !root->right)
18         {
19             s += to_string(root->val);
20             res.push_back(s);
21             return;
22         }
23 
24         s += to_string(root->val) + "->";
25         findTreePaths(root->left, s);
26         findTreePaths(root->right, s);
27     }
28     vector<string> binaryTreePaths(TreeNode* root) 
29     {
30         string s;
31         findTreePaths(root,s);
32         return res;
33     }
34 };

 

posted @ 2020-04-12 22:31  Jinxiaobo0509  阅读(149)  评论(0)    收藏  举报