Getting runtime error on test case [0,4,3,0]


  • 0
    V

    Using this code on custom test case [0,4,3,0] and 0 i get [1,4] which shows correct but on submit solution shows runtime error. please help.

    #include <stdio.h>
    
    #include <stdlib.h>
    
    struct node{
        int index;
        struct node * left;
        struct node * right;
        int balancefactor;
        int value;
    };
    
    void singlerightrotation(struct node **nodeptr)
    {
        struct node *temp = *nodeptr;
        nodeptr = &(*nodeptr)->left;
        temp->left = (*nodeptr)->right;
        (*nodeptr)->right= temp;
        (*nodeptr)->right->balancefactor = ((*nodeptr)->right->left?\
             (*nodeptr)->right->left->balancefactor:0) - ((*nodeptr)->right->right?(*nodeptr)->right->right->balancefactor:0);
    (*nodeptr)->balancefactor = (*nodeptr)->left->balancefactor - (*nodeptr)->right->balancefactor;
    }
    
    void singleleftrotation(struct node **nodeptr)
    {
        struct node *temp = *nodeptr;
        *nodeptr = (*nodeptr)->right;
        temp->right = (*nodeptr)->left;
        (*nodeptr)->left= temp;
        (*nodeptr)->left->balancefactor = ((*nodeptr)->left->left?(*nodeptr)->left->left->balancefactor:0) - \((*nodeptr)->left->right?(*nodeptr)->left->right->balancefactor:0);
        (*nodeptr)->balancefactor = (*nodeptr)->left->balancefactor - (*nodeptr)->right->balancefactor;
    }
    void doubleleftrotation(struct node **nodeptr)
    {
        singlerightrotation(&(*nodeptr)->right);
        singleleftrotation(nodeptr);
    }
    void doublerightrotation(struct node **nodeptr)
    {
        singleleftrotation(&(*nodeptr)->left);
        singlerightrotation(nodeptr);
    }
    void balancetree(struct node **nodeptr)
    {
        if((*nodeptr)->balancefactor == 0)
    	    return;
        if((*nodeptr)->balancefactor < -1)
        {
    	    if((*nodeptr)->right->balancefactor == -1)
    		    singleleftrotation(nodeptr);
    	    else
    		    doubleleftrotation(nodeptr);
        }
        if((*nodeptr)->balancefactor > 1)
        {
    	    if((*nodeptr)->left->balancefactor == 1)
    		    singlerightrotation(nodeptr);
    	    else
    		    doublerightrotation(nodeptr);
        }
    }
    void add(struct node **nodeptr1, struct node **nodeptr2)
    {
        if((*nodeptr2)->value < (*nodeptr1)->value)
        {
            if((*nodeptr1)->left)
                add(&(*nodeptr1)->left,nodeptr2);
            else
                (*nodeptr1)->left = *nodeptr2;
    	    (*nodeptr1)->balancefactor+=1;
    	    balancetree(nodeptr1);
        }
        if((*nodeptr2)->value >= (*nodeptr1)->value)
        {
            if((*nodeptr1)->right)
                add(&(*nodeptr1)->right,nodeptr2);
            else
                (*nodeptr1)->right = *nodeptr2;
    	(*nodeptr1)->balancefactor -= 1;
    	balancetree(nodeptr1);
        }
    }
    void createbst(struct node **root,int value, int index)
    {
        if( *root == NULL)
        {
    	    *root = (struct node*)malloc(sizeof(struct node));
    	    (*root)->value = value;
    	    (*root)->index = index;
    	    (*root)->left = NULL;
    	    (*root)->right = NULL;
    	    (*root)->balancefactor = 0;
        }
        else
        {
            struct node *nodeptr;
    	    nodeptr = (struct node*)malloc(sizeof(struct node));
    	    nodeptr->value = value;
    	    nodeptr->index = index;
    	    nodeptr->left = NULL;
    	    nodeptr->right = NULL;
    	    nodeptr->balancefactor = 0;
    	    add(root, &nodeptr);
        }
    }
    
    void printbst(struct node *nodeptr,struct node **arr)
    {
        static int i = 0;
        if(nodeptr == NULL)
            return;
        printbst(nodeptr->left,arr);
        arr[i++] = nodeptr;
        printbst(nodeptr->right,arr);
    
    }
    
    void destroytree(struct node *nodeptr)
    {
        if(nodeptr == NULL)
            return;
        destroytree(nodeptr->left);
        destroytree(nodeptr->right);
        free(nodeptr);
    }
    
    int* twoSum(int* nums, int numsSize, int target) {
        int i,newsize= 0;
        int j=0;
        int *indexarr = (int*)malloc(sizeof(int)*2);
        struct node *root = NULL;
        struct node **arr;
        for(i=0;i<numsSize;++i)
        {
            if(nums[i] <= target)
            {
                ++newsize;
                createbst(&root,nums[i],i+1);
            }
        }
    
        arr = (struct node **)malloc(sizeof(struct node *)*newsize);
        printbst(root,arr);
        i=0;
        j=newsize-1;
        while(i<newsize && j>=0)
        {
            if(arr[i]->value+arr[j]->value == target)
                break;
            if(arr[i]->value+arr[j]->value > target)
                --j;
            if(arr[i]->value+arr[j]->value < target)
                ++i;
        }
        if(arr[i]->index<arr[j]->index)
        {
            indexarr[0]=arr[i]->index;
            indexarr[1]=arr[j]->index;
        }
        else
        {
            indexarr[0]=arr[j]->index;
            indexarr[1]=arr[i]->index;
        }
        destroytree(root);
        free(arr);
        return indexarr;
    }

Log in to reply
 

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