Simplest C++ solution (ez)


#1
void allP(TreeNode *A, int B, vector<vector<int>> &res, vector<int> ans){
    if(A == nullptr) return;
    ans.push_back(A->val);
    if(A->val == B && ((A->left == nullptr) && (A->right == nullptr))) res.push_back(ans);
    if(A->left != nullptr) allP(A->left, B-(A->val), res, ans); // this way no need of any count variables to keep track
    if(A->right != nullptr) allP(A->right, B-(A->val), res, ans);

}
 
 
vector<vector<int> > Solution::pathSum(TreeNode* A, int B) {
    vector<vector<int>> res;
    vector<int> ans;
    if(A == nullptr) return res;
    allP(A, B, res, ans);
    return res;
}