leetcode 113.路径总和II

这题很简单
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> a;
vector<vector<int>> res;
int l=0;
int summ;
void dfs(TreeNode* r){
if(r==NULL) return;
a.push_back(r->val);
l+=r->val;
vector<int>b=a;
int ll=l;
if(r->left==NULL&&r->right==NULL){
if(l==summ){
res.push_back(a);
return;
}
return;
}
dfs(r->left);
a=b;
l=ll;
dfs(r->right);
a=b;
l=ll;
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
summ=sum;
dfs(root);
return res;
}
};

浙公网安备 33010602011771号