/**
* 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 findPath(TreeNode *root, int curSum, vector<int>&path, int expectSum, vector<vector<int>>&temp)
{
if(root==NULL)
return;
curSum += root->val;
path.push_back(root->val);
bool isLeaf = root->left == NULL && root->right == NULL;
if (isLeaf && curSum == expectSum)
{
/*
vector<int>::iterator iter;
for (iter = path.begin(); iter != path.end(); iter++)
{
cout << *iter << " ";
}
*/
temp.push_back(path);
}
if (root->left)
{
findPath(root->left, curSum, path, expectSum,temp);
}
if (root->right)
{
findPath(root->right, curSum, path, expectSum,temp);
}
curSum -= root->val;
path.pop_back();
}
vector<vector<int> > pathSum(TreeNode *root, int sum)
{
vector<int> path;
vector<vector<int>> result;
int curSum = 0;
findPath(root, curSum, path, sum, result);
return result;
}
};
【LeetCode】Path Sum II
最新推荐文章于 2017-06-29 10:05:00 发布