6ms, The runtime of my solution beats 100% of c submissions


  • 0
    D

    The runtime of my solution beats 100% of c submissions

        void helper(struct TreeNode* root, int h, int *q, int *rsize, int max)
         {
         if(h >= max){
         q = realloc(q, sizeof(int) * 2 * max);
         max = 2 * max;
          }
     if(root == NULL){
         return;
     }
    
     if(h > *rsize){
         *rsize = h;
         q[h] = root->val;
     }else{
         if(root->val > q[h]){
             q[h] = root->val;
         }
     }
     if(root->left){
         helper(root->left, h+1, q, rsize, max);
     }
     if(root->right){
         helper(root->right, h+1, q, rsize, max);
     }
     }
     int* largestValues(struct TreeNode* root, int* returnSize) {
    int *q = malloc(1000*sizeof(int));
    memset(q, 0, sizeof(int)*1000);
    *returnSize = -1;
    helper(root, 0, q, returnSize, 1000);
    *returnSize = *returnSize + 1;
    return q;
    }

Log in to reply
 

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