/**
* 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;
if (!root) {
return res;
}
vector<int> record;
pathSum(res, record, root, sum);
return res;
}
private:
void pathSum(vector<vector<int>>& res, vector<int> record, TreeNode* root, int sum) {
if (!root && sum == 0) {
res.push_back(record);
return;
}
record.push_back(root->val);
if (!root->left && !root->right && sum == root->val) {
res.push_back(record);
return;
}
if (root->left) {
pathSum(res, record, root->left, sum - root->val);
}
if (root->right) {
pathSum(res, record, root->right, sum - root->val);
}
}
};