/**
* Definition for binary tree
* 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<int> tempvec;
vector<vector<int>> allpath;
sps(root, sum, tempvec, allpath);
return allpath;
}
// train of thought
//
void sps(TreeNode* root, int sum, vector<int> &pvec, vector<vector<int>> &allpath) {
if(!root) return;
if(root->val == sum && !root->left && !root->right) {
pvec.push_back(root->val);
allpath.push_back(pvec);
pvec.pop_back();
return;
}
pvec.push_back(root->val);
sps(root->left, sum-root->val, pvec, allpath);
sps(root->right, sum-root->val, pvec, allpath);
pvec.pop_back();
}
};
欢迎关注微信公众号——计算机视觉: