Description
Submission
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int> card;
backtrack(root, sum, card, res);
return res;
}
private:
void backtrack(TreeNode* root, int sum, vector<int>& card, vector<vector<int>>& res) {
if(root == NULL) {
return;
}
if(!root->left && !root->right && sum == root->val) {
card.push_back(root->val);
res.push_back(card);
card.pop_back();
return;
}
card.push_back(root->val);
backtrack(root->left, sum - root->val, card, res);
backtrack(root->right, sum - root->val, card, res);
card.pop_back();
}
};