```
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int> out;
dfs(root,sum,res,out);
return res;
}
void dfs(TreeNode* root,int cur_sum,vector<vector<int>> &res,vector<int>& out){
if(!root) return;
out.push_back(root->val);
cur_sum-=root->val;
if(!root->left&&!root->right&&cur_sum==0){
res.push_back(out);
return;
}
if(root->left){
dfs(root->left,cur_sum,res,out);
out.pop_back();
}
if(root->right){
dfs(root->right,cur_sum,res,out);
out.pop_back();
}
}
};
```