C++ Approach, tell me how to make it more efficient/optimize


#1
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
void solve(TreeNode*root,int sum,vector<vector<int>>&ans,vector<int>sub)
{
    if(!root)
        return;
    int subSum = sum - root->val;
    sub.push_back(root->val);
    if(!root->left && !root->right && subSum == 0)
    {
        ans.push_back(sub);
        return;
    }
    
    solve(root->left,subSum,ans,sub);
    solve(root->right,subSum,ans,sub);
    sub.pop_back();
}
vector<vector<int> > Solution::pathSum(TreeNode* A, int B) 
{
    vector<vector<int>> ans;
    vector<int> sub;
    solve(A,B,ans,sub);
    return ans;
}