/**
* 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<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int> > res;
vector<int> path;
helper(root,sum,path,res);
return res;
}
void helper(TreeNode* root, int sum, vector<int> path, vector<vector<int> >& res)
{
if(!root)
return;
path.push_back(root->val);
if(!root->left&&!root->right)
{
if(root->val==sum)
res.push_back(path);
}
else if(!root->right)
helper(root->left,sum-root->val,path,res);
else if(!root->left)
helper(root->right,sum-root->val,path,res);
else
{
helper(root->left,sum-root->val,path,res);
helper(root->right,sum-root->val,path,res);
}
}
};
08-12
08-12
08-12
08-12