I got the OLE when I passing a reference as the argument, but passing a value is ok.


  • 0
    H
    class Solution {
    public:
    vector<vector<int>> res;
    void helper(TreeNode* root, int sum, vector<int>& path){   //output limit exceeded
        if(!root) return;
        if(root->val == sum && !root->left && !root->right){
            path.push_back(sum);
            res.push_back(path);
            return;
        }
        path.push_back(root->val);
        helper(root->left,sum-root->val,path);
        helper(root->right,sum-root->val,path);
    }
     
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<int> path;
        helper(root, sum, path);
        return res;
    }
    };
    
    helper(TreeNode* root, int sum, vector<int> path)  is ok.
    

    Anyone can explain it? thx


  • 0
    D

    Everytime you call "path.push_back", you have to call path.pop_back() to recover the path to its original state


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.