leetcode 113. 路径总和 II

时间:2023-02-22 07:57:50


递归到叶子才将tans推进去ret
ret.push_back(tans);

class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> ret;
findw(root,sum,{},ret);
return ret;
}
void findw(TreeNode* root, int sum,vector<int> tans,vector<vector<int>>&ret){
if(!root){
return;
}
if(root->val == sum && !root->left && !root->right){
tans.push_back(root->val);
ret.push_back(tans);
}else{
tans.push_back(root->val);
if(root->left){
findw(root->left,sum - root->val,tans,ret);
}
if(root->right){
findw(root->right,sum - root->val,tans,ret);
}
}
}

};

leetcode 113. 路径总和 II