很好的一道深搜题
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>>ret;
vector<int>path;
void find(TreeNode*root,int count)
{
if(!root->left&&!root->right&&count==0)
{
ret.push_back(path);
return;
}
if(!root->left&&!root->right)
{
return;
}
if(root->left)
{
path.push_back(root->left->val);
count-=root->left->val;
find(root->left,count);
count+=root->left->val;
path.pop_back();
}
if(root->right)
{
path.push_back(root->right->val);
count-=root->right->val;
find(root->right,count);
count+=root->right->val;
path.pop_back();
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(root==nullptr)
{
return ret;
}
path.push_back(root->val);
find(root,targetSum-root->val);
return ret;
}
};