/**
* 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<int> path;
vector<vector<int> > paths;
vector<vector<int>> pathSum(TreeNode* root, int sum) {
findPaths(root, sum);
return paths;
}
private:
void findPaths(TreeNode* n, int sum) {
if (n == NULL) return;
path.push_back(n->val);
if (n->left == NULL && n->right == NULL && sum == n->val) {
paths.push_back(path);
}
if (n->left) findPaths(n->left, sum - n->val);
if (n->right) findPaths(n->right, sum - n->val);
path.pop_back();
}
};