Memory limit exceeded in construct tree from post order and inorder


  • 0
    D
    `TreeNode * cal(vector<int> inorder,vector<int> postorder,int f, int l ,int *len) {
        if(f>l)
            return NULL;
        if(*len ==-1)
            return NULL;
        int i;
        TreeNode *temp = (struct TreeNode *)malloc(sizeof(struct TreeNode));
        for(i=f;i<=l;i++) {
            if(inorder[i] == postorder[*len]) {
                temp->val = inorder[i];
                *len = *len -1;
                temp->right = cal(inorder,postorder,i+1,l,len);
                temp->left = cal(inorder,postorder,f,i-1,len);
                return temp;
            }
        }
        return NULL;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size()==0)
            return NULL;
        int len = postorder.size() -1;
        struct TreeNode * root = cal(inorder,postorder,0,len,&len);
        return root;
    }

  • 0
    Q

    The definition of function cal need to be vector<int>&, not vector<int>.


  • 0
    D

    thanks for the help


Log in to reply
 

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