/**
* 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>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> ans;
vector<int> cur;
pathSum(root, targetSum, cur, ans);
return ans;
}
private:
void pathSum(TreeNode *root, int gap, vector<int> &cur, vector<vector<int>> &ans) {
if(root == nullptr)
return;
cur.push_back(root->val);
if(root->left == nullptr && root->right == nullptr)
if(gap == root->val)
ans.push_back(cur);
pathSum(root->left, gap - root->val, cur, ans);
pathSum(root->right, gap - root->val, cur, ans);
cur.pop_back();
}
};