Simple recursive solution in cpp


#1
void build(TreeNode* a, int b, vector<vector<int>> &ans, vector<int> curr)
{
    if(!a) return;
    curr.push_back(a->val);
    b -= a->val;
    if(!a->left && !a->right)
    {
        if(b == 0) 
        {
            ans.push_back(curr);
        }
        return;
    }
    if(a->left) build(a->left, b,ans,curr);
    if(a->right) build(a->right, b,ans,curr);
}
vector<vector<int> > Solution::pathSum(TreeNode* a, int b) {
    vector<vector<int>> ans;
    build(a,b,ans,{});
    return ans;
}

#2

what is the use to make 2 functions? We can write it in single function.