/**
* 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>> all_path;
vector<int> path;
if (!root) return all_path;
int temp_sum = 0;
find_path_sum(root, &all_path, &path, temp_sum, sum);
return all_path;
}
private:
void find_path_sum(TreeNode* root, vector<vector<int>>* all_path, vector<int>* path, int temp_sum, int sum) {
if (!root) return;
path->push_back(root->val);
temp_sum += root->val;
if (!root->left && !root->right && temp_sum == sum) {
all_path->push_back(*path);
}
find_path_sum(root->left, all_path, path, temp_sum, sum);
find_path_sum(root->right, all_path, path, temp_sum, sum);
path->pop_back();
}
};