/**
* 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>> result;
vector<int> path;
int now_sum = 0;
void myPathSum(TreeNode* root, int targetSum) {
if(!root) return ;
path.push_back(root->val);
now_sum += root->val;
if(!root->left && !root->right && now_sum == targetSum) result.push_back(path);
myPathSum(root->left, targetSum);
myPathSum(root->right, targetSum);
path.pop_back();
now_sum -= root->val;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
myPathSum(root, targetSum);
return result;
}
};
05-02
319