class Solution {
public:
void dfs(vector<string>& res,string str,TreeNode* root) {
if(!root) return;
if(!root->left&&!root->right) {
str+=to_string(root->val);
res.push_back(str);
return;
}
str = str + to_string(root->val) + "->";
dfs(res,str,root->left);
dfs(res,str,root->right);
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
string str="";
dfs(res,str,root);
return res;
}
};
Leetcode257(力扣257):二叉树的所有路径
最新推荐文章于 2024-06-12 10:00:23 发布