class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum)
{
vector<vector<int> >vec;
vector<int> path;
if(root==NULL)
return vec;
FindPathSum(root, sum, 0, vec, path);
return vec;
}
void FindPathSum(TreeNode *root, int sum, int currentsum, vector<vector<int> > &vec, vector<int> &path)
{
currentsum += root->val;
path.push_back(root->val);
bool isLeaf = root->left==NULL && root->right==NULL;
if(currentsum==sum && isLeaf)
{
vec.push_back(path);
}
if(root->left!=NULL)
FindPathSum(root->left, sum, currentsum, vec, path);
if(root->right!=NULL)
FindPathSum(root->right, sum, currentsum, vec, path);
currentsum -= root->val;
path.pop_back();
}
};
LeetCode-Path Sum II
最新推荐文章于 2019-04-07 22:09:40 发布