class Solution {

public:

```
vector<vector<int>> res;
vector<int> out;
void getAllPaths(TreeNode* root, int sum, int sum1){
if(root == NULL)
return;
else{
sum1 += root->val;
out.push_back(root->val);
if(root->left)
getAllPaths(root->left, sum, sum1);
if(root->right)
getAllPaths(root->right, sum, sum1);
if(sum == sum1 && root->left == NULL && root->right == NULL){
if(sum == sum1)
res.push_back(out);
}
out.pop_back();
return;
}
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
getAllPaths(root, sum, 0);
return res;
}
```

};