Using recursion :-)


#1
void get_me_ans(TreeNode * root,int B ,int s,vector<int>temp, vector<vector<int>>& ans)

{
if(root==NULL)
return ;

s+= root->val;

temp.push_back(root->val);

if(s==B and root->left==NULL and root->right==NULL )
{
    ans.push_back(temp);
    return;
}

if(root->left!=NULL)
get_me_ans(root->left,B,s,temp,ans);

if(root->right!=NULL)
get_me_ans(root->right,B,s,temp,ans);

}
vector<vector > Solution::pathSum(TreeNode* A, int B) {

vector<vector<int>>ans;
vector<int>temp;

get_me_ans(A,B,0,temp,ans);

return ans;

}