Runtime error But runs locally


  • 0
    G
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    /**
     * Return an array of arrays of size *returnSize.
     * The sizes of the arrays are returned as *columnSizes array.
     * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
     */
     
     typedef struct ListNode2
     {
        struct  TreeNode *val;
        struct ListNode2 *next;
     } ListNode2;
     
     typedef struct Queue
     {
         ListNode2 *front;
         ListNode2 *rear;
     } Queue;
     
     void enqueue(Queue *Q, struct TreeNode *data)
     {
         ListNode2 *newNode = (ListNode2 *)malloc(sizeof(ListNode2));
         newNode->val = data;
         newNode->next  = NULL;
         
         if(Q->front == NULL && Q->rear == NULL)
            Q->front = Q->rear = newNode;
        else
        {
            Q->rear->next = newNode;
            Q->rear = newNode;
        }
     }
    
    
    struct TreeNode *dequeue(Queue *Q)
    {
        
        
        if(Q->front == NULL)
            return NULL;
        
        struct TreeNode *temp = Q->front->val;
        Q->front = Q->front->next;
        
        if(Q->front == NULL)
            Q->rear = NULL;
            
        return temp;
    }
    
    bool isQueueEmpty(Queue *Q)
    {
        if(Q == NULL || Q->front == NULL || Q->front == NULL)
            return true;
        return false;
    }
    
    int** levelOrder(struct TreeNode* root, int** columnSizes, int* returnSize) 
    {
        
        const int MAX = 100;
        
        Queue *Q = (Queue *)malloc(sizeof(Queue));
        Q->front = Q->rear = NULL;
        
        int **res = (int **)malloc(sizeof(int *) * MAX);
        *columnSizes = (int *)malloc(sizeof(int) * MAX);
        
        
        if(root == NULL)
        {
            *returnSize = 0;
            return res;
        }
        
        enqueue(Q,root);
        enqueue(Q,NULL);
        
        int l = 0;
        res[0] = (int *)malloc(sizeof(int) * 1);
        
        int nodeCount = 0;
        
        while(!isQueueEmpty(Q))
        {
            root = dequeue(Q);
            if(root)
            {
                res[l][nodeCount++] = root->val;
                
                if(root->left)
                    enqueue(Q,root->left);
                if(root->right)
                    enqueue(Q,root->right);
            }
            else
            {
                (*columnSizes)[l] = nodeCount;
                
                if(!isQueueEmpty(Q))
                {
                    l++;
                    res[l] = (int *)malloc(sizeof(int) * pow(l,2));
                    nodeCount = 0;
                    enqueue(Q,NULL);
                }
            }
        }
        
        *returnSize = l+1;
        return res;
        
    }

  • 0
    D

    can you paste your input and outputs ?


Log in to reply
 

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