Easy C++ solution in O(n) time complexity and O(h) space complexity


#1
void helper(TreeNode *root,int curr,int B,vector<int>tmp,vector<vector<int>>&ans)
{
    if(!root)
    {
        return;
    }
    curr+=root->val;
    tmp.push_back(root->val);
    if(!root->left && !root->right && curr==B)
    {
        ans.push_back(tmp);
        return;
    }
    helper(root->left,curr,B,tmp,ans);
    helper(root->right,curr,B,tmp,ans);
}
vector<vector<int> > Solution::pathSum(TreeNode* A, int B) 
{
    vector<vector<int>>v;
    helper(A,0,B,{},v);
    return v;
}