Very Simple Recursive solution in cpp


#1
vector<vector<int> > Solution::pathSum(TreeNode* A, int B) {
    vector<vector<int>> v;
    if(A==NULL)
    return v;
    if(!A->right && !A->left)
    {
        if(A->val==B)
        {v.push_back({A->val});
            return v;
        }
        else
        return v;
        
        
    }
    vector<vector<int>> l = pathSum(A->left, B-A->val);
    vector<vector<int>> r = pathSum(A->right, B-A->val);
    
    if(l.size()>0)
    {
        for(int i=0;i<l.size();i++)
       { 
           std::reverse(l[i].begin(),l[i].end());
           l[i].push_back(A->val);
            std::reverse(l[i].begin(),l[i].end());
           v.push_back(l[i]);
           
       }
    }    
     if(r.size()>0)
    {
        for(int i=0;i<r.size();i++)
        {
           std::reverse(r[i].begin(),r[i].end());
           r[i].push_back(A->val);
           std::reverse(r[i].begin(),r[i].end());
           v.push_back(r[i]);
        }
    } 
    
    return v;
}