Solved in C++( recursive approach)


#1

int sum(vector&v)
{
int i,c=0;
for(i=0;i<v.size();i++)
{
c+=v[i];
}
return c;
}
void helper(TreeNode A,vector<vector>&v,vector&aux,int B)
{
if(A==NULL)
{
return;
}
aux.push_back(A->val);
if(A->left==NULL && A->right==NULL)
{
if(sum(aux)==B)
{
v.push_back(aux);
}
}
else
{
helper(A->left,v,aux,B);
helper(A->right,v,aux,B);
}
aux.pop_back();
}
vector<vector > Solution::pathSum(TreeNode
A, int B)
{
if(A==NULL)
{
return {{}};
}
vector<vector>v;
vectoraux;
helper(A,v,aux,B);
aux.clear();
return v;
}