/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void SumAll(TreeNode *root,int sum,int cursum,vector<int> &tmp,vector<vector<int>> &vec)
{
if(root==NULL)
return ;
tmp.push_back(root->val);
if((root->left==NULL)&&(root->right==NULL))
{
if(cursum+root->val==sum)
{
vec.push_back(tmp);
}
}//别判断了
SumAll(root->left,sum,cursum+root->val,tmp,vec);
SumAll(root->right,sum,cursum+root->val,tmp,vec);
tmp.pop_back();//删除当前vector的最后一个元素
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> ret;
if(root==NULL) return ret;
vector<int> a;
SumAll(root,sum,0,a,ret);
return ret;
}
};
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void SumAll(TreeNode *root,int sum,int cursum,vector<int> &tmp,vector<vector<int>> &vec)
{
if(root==NULL)
return ;
tmp.push_back(root->val);
if((root->left==NULL)&&(root->right==NULL))
{
if(cursum+root->val==sum)
{
vec.push_back(tmp);
}
}//别判断了
SumAll(root->left,sum,cursum+root->val,tmp,vec);
SumAll(root->right,sum,cursum+root->val,tmp,vec);
tmp.pop_back();//删除当前vector的最后一个元素
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> ret;
if(root==NULL) return ret;
vector<int> a;
SumAll(root,sum,0,a,ret);
return ret;
}
};