LeetCode257.二叉树的所有路径
题目
本题未成功做出,字符串相关内容不会
1 class Solution { 2 public: 3 void constructpath(TreeNode* root,string path,vector<string>&paths){ 4 if(root!=NULL){ 5 path = path + to_string(root->val); 6 if(root->left == NULL && root->right == NULL) paths.push_back(path); 7 else{ 8 path = path + "->"; 9 constructpath(root->left,path,paths); 10 constructpath(root->right,path,paths); 11 } 12 } 13 14 } 15 vector<string> binaryTreePaths(TreeNode* root) { 16 vector<string>paths; 17 constructpath(root,"",paths); 18 return paths; 19 } 20 21 };

浙公网安备 33010602011771号