Request for help - Memory Limit Exceeded

  • 0

    When I submitted the below solution, I see that all test cases have passed but it exceeds the memory constraint for this problem. Although I do not see any mention about memory in the problem statement. Can someone please help me explain why this does not meet the memory constraints? Thanks in advance!

    202 / 202 test cases passed.
    Status: Memory Limit Exceeded

    class Solution {
        int search(vector<int> inorder, int in_st, int in_end, int val)
            int i = -1;
            for(i = in_st; i <= in_end; ++i)
                if(inorder[i] == val)
            return i;
        TreeNode* maketree(vector<int> inorder, vector<int> postorder, int in_st, int in_end, int &post_idx)
            if(in_st > in_end)
                return nullptr;
            TreeNode *node = new TreeNode(postorder[post_idx--]);
            if(in_st == in_end)
                return node;
            int in_idx = search(inorder, in_st, in_end, node->val);
            node->right = maketree(inorder, postorder, in_idx+1, in_end, post_idx);
            node->left = maketree(inorder, postorder, in_st, in_idx-1, post_idx);
            return node;
        TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) 
            if(inorder.size() != postorder.size() || (inorder.size() == 0))
                return nullptr;
            int n = inorder.size(), post_idx = n-1;
            return maketree(inorder, postorder, 0, n-1, post_idx);

Log in to reply

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