struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void pathSum(TreeNode *n, int sum, vector<int>& nums, vector<vector<int> > &results) {
if (n == NULL) return;
int val = n->val;
nums.push_back(val);
sum -= val;
if (n->left == NULL && n->right == NULL && sum == 0) {
results.push_back(nums);
} else {
pathSum(n->left, sum, nums, results);
pathSum(n->right, sum, nums, results);
}
nums.erase(nums.end()-1);
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > results;
vector<int> nums;
pathSum(root, sum, nums, results);
return results;
}