/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private: vector<vector<int> > sumpath;
int refsum;
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sumpath.clear();
if(root==NULL)
return sumpath;
refsum = sum;
vector<int> path;
allpath(root,0,path);
return sumpath;
}
void allpath(TreeNode *root,int pre,vector<int> path)
{
if(root->left==NULL&&root->right==NULL)
{
if(pre+root->val==refsum)
{
path.push_back(root->val);
sumpath.push_back(path);
}
}
path.push_back(root->val);
if(root->left!=NULL)
{
allpath(root->left,pre+root->val,path);
}
if(root->right!=NULL)
{
allpath(root->right,pre+root->val,path);
}
}
};