class Solution {
public:
void construct_paths(TreeNode* root,string path,vector<string> &paths)
{
if(root)
{
path += to_string(root->val);
if(!root->left&&!root->right)
{
paths.push_back(path);
}
else
{
path += "->";
construct_paths(root->left,path,paths);
construct_paths(root->right,path,paths);
}
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> paths;
construct_paths(root,"",paths);
return paths;
}
};
leetcode每日一题第九十四天
最新推荐文章于 2024-08-21 10:11:50 发布