Day133 | 灵神 | 回溯算法 | 子集型 二叉树的所有路径
257.二叉树的所有路径
思路:
这道题思路比较简单,直接写就行
完整代码:
class Solution {
public:
vector<string> res;
void dfs(TreeNode *t,string path)
{
if(t==nullptr)
return ;
path += to_string(t->val);
if(t->left==nullptr&&t->right==nullptr)
{
res.push_back(path);
return ;
}
path += "->";
dfs(t->left,path);
dfs(t->right,path);
}
vector<string> binaryTreePaths(TreeNode* root) {
string path;
dfs(root,path);
return res;
}
};
814

被折叠的 条评论
为什么被折叠?



