https://leetcode.com/problems/path-sum-ii/
/** * 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) { if(root==NULL) return res; vector<int> temp; work(root,sum,temp); return res; } vector<vector<int>> res; void work(TreeNode * root,int sum,vector<int> temp){ temp.push_back(root->val); if(root->left==NULL&&root->right==NULL) { if(sum==root->val) { res.push_back(temp); } return ; } if(root->left!=NULL) { work(root->left,sum-(root->val),temp); } if(root->right!=NULL) { work(root->right,sum-(root->val),temp); } } };