Runtime error 'C'; local run is fine


  • 0
    D
    /**
     * 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().
     */
    
    struct TreeNode * new_node(int val)
    {
      struct TreeNode *n = (struct TreeNode*)malloc(sizeof(struct TreeNode));
      n->val = val;
      n->left = NULL;
      n->right = NULL;
      return n;
    }
    
    int max(int a, int b)
    {
         if (a>b) {
             return a;
         }
         return b;
    }
    int height(struct TreeNode *node)
    {
         if (node == NULL) {
             return 0;
         }
         return 1+ max(height(node->left), height(node->right));
    }
    
    void _xlevelOrderBottom(struct TreeNode *root, int level, int **result, int **columnSizes, int row, int *col)
    {
         if (root == NULL) {
             return;
         }
         if (level == 1) {
             result[row][(*col)] = root->val;
             (*col)++;
         } else if (level >=1){
           _xlevelOrderBottom(root->left, level-1, result, columnSizes, row, col);
           _xlevelOrderBottom(root->right, level-1, result, columnSizes, row, col);
         }
    }
    
    int** levelOrder(struct TreeNode* root, int **columnSizes, int* returnSize)
    {
      int i, j, col, level;
      (*returnSize) = 0;
      level = height(root);
      int **result = (int**)malloc(sizeof(int*)*level);
      for (i=0; i<level; i++) {
        result[i] = (int*)malloc(sizeof(int)*level);
      }
      *columnSizes = (int*)malloc(sizeof(int*)*level);
      
     printf("\nLEVEl %d\n", level);
      for (i=1; i<=level; i++) {
        col = 0;
        _xlevelOrderBottom(root, i, result, columnSizes, i-1, &col);
        (*columnSizes)[i-1] = col;
      }
      (*returnSize) = level;
    
      printf("\n");
      for (i=0; i<*returnSize; i++) {
        printf("\n");
        for (j=0; j<(*columnSizes)[i]; j++) {
          printf("%d ", result[i][j]);
        }
      }
      printf("\n");
    
      return result;
    }
    

    and the error is "runtime error"

    Do you think that my mallocs are incorrect ?


  • 0
    D

    HERE IS THE INPUT (by OJ):

    [69,73,68,18,20,18,39,7,-3,13,-1,42,5,93,70,63,17,null,91,-4,30,null,-1,64,-4,16,49,48,78,51,43,92,45,null,53,9,36,80,-6,58,78,null,null,41,81,89,67,71,null,25,null,82,54,28,14,61,57,35,5,83,9,18,null,-9,-9,50,92,93,null,0,80,62,1,28,29,27,89,21,null,85,-9,null,56,56,-9,null,null,43,null,29,97,-7,null,35,25,90,67,53,18,61,7,23,81,37,19,26,2,0,19,null,null,77,37,-2,null,49,39,28,1,37,11,87,83,68,55,53,33,-2,22,7,52,null,14,null,18,50,97,-8,-7,null,21,59,72,27,null,64,null,null,47,null,null,38,46,null,null,99,null,null,48,13,85,78,7,64,43,59,71,11,37,12,37,50,2,null,null,89,87,null,78,97,null,31,86,37,96,34,38,6,36,null,null,99,63,null,12,null,82,null,81,70,19,null,81,32,null,null,null,null,79,10,null,91,48,-3,94,65,null,20,26,96,21,92,91,null,89,9,74,null,null,96,null,64,67,50,96,null,null,null,null,null,null,40,78,null,27,3,17,null,null,2,45,null,null,null,null,null,13,null,null,17,45,69,30,null,null,43,null,4,13,-6,66,6,null,16,48,55,98,69,57,null,5,9,65,-9,55,2,null,null,null,null,null,null,68,null,null,null,5,61,51,null,null,32,43,null,35,20,null,-7,38,30,1,80,null,null,42,86,42,null,null,null,null,47,null,null,null,62,29,-9,83,60,71,48,null,24,null,76,6,65,18,95,29,11,null,38,null,null,null,null,21,3,6,23,36,null,45,null,34,null,null,null,null,null,null,41,null,57,13,18,92,43,83,null,null,null,null,null,null,null,2,-4,97,null,93,null,62,null,null,48,18,71,92,53,89,null,null,null,95,null,16,null,null,null,83,87,5,null,null,3,-8,-4,65,null,null,null,22,null,31,null,null,null,63,null,null,62,null,57,12,85,45,23,55,null,null,null,81,83,23,null,3,null,83,null,-4,null,null,null,null,null,64,null,15,50,57,null,null,null,4,null,null,null,29,null,null,87,null,22,92,null,null,67,90,null,93,47,46,null,null,null,28,72,18,59,25,3,74,null,null,null,-5,28,-1,61,15,null,null,null,null,79,null,16,null,null,59,47,-7,98,31,50,null,null,null,null,19,null,93,null,22,null,null,-5,40,null,null,null,75,30,null,7,53,76,null,null,null,null,null,68,19,null,63,41,91,null,43,null,49,null,null,null,null,null,46,null,null,87,74,49,1,21,62,6,34,77,null,null,null,null,null,null,-9,61,null,null,null,7,null,45,null,null,63,null,null,7,null,null,16,86,null,null,63,null,61,72,null,13,null,24,91,null,null,59,null,null,48,14,77,null,null,null,null,92,null,null,null,null,null,null,84,null,null,76,82,63,84,84,94,null,null,null,null,null,47,40,null,null,null,null,75,20,null,null,null,-9,null,null,24,74,null,51,null,null,91,null,83,17,null,null,null,42,49,88,57,85,1,null,94,null,28,36,78,null,53,null,27,25,46,97,58,null,null,null,null,null,null,null,null,12,33,null,null,6,null,null,null,87,null,null,null,null,null,null,null,9,null,83,null,null,null,90,11,null,61,null,89,null,46,null,86,81,null,null,null,null,null,null,null,53,null,null,59,null,null,null,null,null,null,null,29,null,47,97,0,null,null,null,null,9,null,17,null,91,45,9,61,21,null,null,64,null,69,null,44,null,null,null,null,12,null,2,-8,88,null,null,null,null,null,-8,null,93,null,null,null,86,null,null,97,null,null,null,null,72,null,null,null,null,null,50,null,null,null,null,null,47,70,null,62,null,-3,-5,59,15,null,-3,37,null,null,null,null,20,-2,null,8,90,null,null,null,61,null,null,null,null,null,null,null,15,12,95,null,null,73,11,76,76,49,null,null,51,null,null,null,null,null,null,null,null,null,null,null,null,null,42,null,null,-9,null,null,null,null,null,null,null,null,80,null,null,70,31,78,98,null,null,null,null,null,null,null,null,null,null,null,null,7,null,null,null,null,57,null,null,null,null,-3,null,null,-7,null,31,42,null,null,null,null,62,17,7,null,null,63,null,null,null,null,83,51,null,76,77,null,null,40,null,null,95,null,27,55,61,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,95,null,93,19,null,37,null,73,null,null,null,null,null,75,null,null,null,null,null,22,null,null,null,null,null,-7,99,null,null,null,null,null,94,63,null,null,null,null,null,null,null,39,77,null,-2,15,null,69,33,9,null,null,null,null,null,null,null,null,null,42,null,null,null,69,35,null,36,null,11,null,null,null,52,null,null,null,null,null,null,null,51,50,null,null,null,null,null,null,30,null,null,null,null,null,63,null,null,null,null,null,null,56,28]

    HERE is the output:

    LEVEl 20

    69
    73 68
    18 20 18 39
    7 -3 13 -1 42 5 93 70
    63 17 91 -4 30 -1 64 -4 16 49 48 78 51 43
    92 45 53 9 36 80 -6 58 78 41 81 89 67 71 25 82 54 28 14 61 57 35 5
    83 9 18 -9 -9 50 92 93 0 80 62 1 28 29 27 89 21 85 -9 56 56 -9 43 29 26 2 0 19 77 37 -2 49 39 28 1 37 11 87
    26 2 0 19 77 37 -2 49 39 28 1 37 11 87 83 68 55 53 33 -2 22 7 52 14 31 86 37 96 34 38 6 36 99 63 12 82 81 70 19 81 32 79 10 91 48 -3 94 65 5 61 51 32 43 35 20 -7 38
    31 86 37 96 34 38 6 36 99 63 12 82 81 70 19 81 32 79 10 91 48 -3 94 65 5 61 51 32 43 35 20 -7 38 30 1 80 42 86 42 47 62 29 -9 83 60 71 48 24 81 83 23 3 83 -4 64 15 50 57 4 29 87 22 92 67 90 93 47 46 28
    5 61 51 32 43 35 20 -7 38 30 1 80 42 86 42 47 62 29 -9 83 60 71 48 24 81 83 23 3 83 -4 64 15 50 57 4 29 87 22 92 67 90 93 47 46 28 72 18 59 86 63 61 72 13 24 91 59 48 14 77 92 84 76 82 63 84 84 94 47 40 75 20 -9 29 47 97 0
    81 83 23 3 83 -4 64 15 50 57 4 29 87 22 92 67 90 93 47 46 28 72 18 59 86 63 61 72 13 24 91 59 48 14 77 92 84 76 82 63 84 84 94 47 40 75 20 -9 29 47 97 0 9 17 91 45 9 61 21 64 69 44 12 2 -8 88 -8 93 86 97 72 50 42 -9
    86 63 61 72 13 24 91 59 48 14 77 92 84 76 82 63 84 84 94 47 40 75 20 -9 29 47 97 0 9 17 91 45 9 61 21 64 69 44 12 2 -8 88 -8 93 86 97 72 50 42 -9 80 70 31 78 98 7 57 -3 -7 31 42
    29 47 97 0 9 17 91 45 9 61 21 64 69 44 12 2 -8 88 -8 93 86 97 72 50 42 -9 80 70 31 78 98 7 57 -3 -7 31 42 62 17 7 63 83 51 76 77 40 95
    42 -9 80 70 31 78 98 7 57 -3 -7 31 42 62 17 7 63 83 51 76 77 40 95 27 95 93
    95 93 19 37 73 75 22 -7 99 94 63 39 77 -2 15
    69 33 9 42 69 35 36 11 52
    51 50 30
    63
    56
    28


  • 0
    D

    I have been using this main() to test my code:

    int main()
    {
    int i, j;
    struct TreeNode root = NULL;
    root = new_node(1);
    /

    struct TreeNode *a = root;
    for (int i=-1; i>=-150; i--) {
    a->left = new_node(i);
    a=a->left;
    }
    a = root;
    for (int i=1; i<=600; i++) {
    a->right = new_node(i);
    a=a->right;
    }
    /
    /

    root->left = new_node(9);
    root->right = new_node(20);
    root->right->left = new_node(15);
    root->right->right= new_node(7);
    */
    int **result = NULL;
    int *colSize = NULL;
    int returnSize = 0;

    result = over_levelOrder(root, &colSize, &returnSize);

    printf("\nreturnSize=%d", returnSize);
    for (i=0; i<returnSize; i++) {
    printf("\nAT-LEVEL=%d\n", i);
    for (j=0; j<colSize[i]; j++) {
    printf("VALUE=%d ", result[i][j]);
    }
    printf("\n");
    }
    for (i=0; i<returnSize-1; i++) {
    free(result[i]);
    }
    free(result);
    free(colSize);
    return 0;
    }


  • 0
    D

    Actually I got it, the sizeof() mallocs weren't correct:

    int** levelOrder(struct TreeNode* root, int **columnSizes, int* returnSize)
    {
      int level;
      (*returnSize) = 0;
      level = height(root);
      int MAX = level*level;
      int **result = (int**)malloc(sizeof(int*)*MAX);
      *columnSizes = (int*)malloc(sizeof(int)*MAX);
    
      printf("\nLEVEl %d\n", level);
      int *tmp_result = (int*)malloc(sizeof(int)*MAX);
      for (int i=1; i<=level; i++) {
        int tmp_col = 0;
        _ylevelOrderBottom(root, i, tmp_result, &tmp_col);
        (*columnSizes)[i-1] = tmp_col;
        result[i-1] = (int*)malloc(sizeof(int)*tmp_col);
        for (int k=0; k<tmp_col; k++) {
          result[i-1][k] = tmp_result[k];
        }
      }
      (*returnSize) = level;
      
      printf("\n[");
      for (int i=0; i<*returnSize; i++) {
        printf("\n[");
        for (int j=0; j<(*columnSizes)[i]; j++) {
          printf("%d ", result[i][j]);
        }
        printf("]");
      }
      printf("\n]");
    
      return result;
    }

Log in to reply
 

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