```
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > result;
vector<int> cur_path(0);
pathSumRec(root, sum, result, cur_path);
return result;
}
// pass the current path as a reference and remember to pop out the last added element
// this improves the performance by 5 times
void pathSumRec(TreeNode* root, int sum, vector<vector<int> >& result, vector<int>& cur_path) {
if (root == NULL) {
return;
}
if (root->val == sum && root->left == NULL && root->right == NULL) {
cur_path.push_back(root->val);
result.push_back(cur_path);
cur_path.pop_back();
return;
}
int sum_left = sum - root->val;
cur_path.push_back(root->val);
pathSumRec(root->left, sum_left, result, cur_path);
//cur_path.pop_back();
pathSumRec(root->right, sum_left, result, cur_path);
cur_path.pop_back();
}
```