您的位置:首页 > 编程语言 > Go语言

LeetCode-Path Sum II

2014-09-21 11:55 316 查看
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();
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Algorithm