Help! I cannot find out where the bug is?


  • 0
    C

    int result[1000];
    int i=-1;

    struct TreeNode* queue[1000];
    int p=-1;
    int q=-1;

    int *_rightSideView(struct TreeNode *root, int *n){
    int lay=0;

    if(!root)   return NULL;
    queue[++p]=root;
    
    while(q!=p){
        struct TreeNode *temp;
        
        temp=queue[++q];
        if(temp->left)  queue[++p]=temp->left;
        if(temp->right) queue[++p]=temp->right;
        if(q==lay){
            *n=*n+1;
            result[++i]=queue[lay]->val;
            lay=p;
        }
    }
    return result;
    

    }

    int *rightSideView(struct TreeNode *root, int *n) {
    *n=0;
    return _rightSideView(root,n);
    }


  • 0
    H

    the test case assume the pointer returned from the function is malloced, which can be freed.

    try this pattern:

    p = malloc(size);
    if (size exceed) {
      p = realloc(p, newSize);
    }
    

  • 0
    C

    Sorry, I do not get it? Finally, I solve it by C++. But still thanks.


Log in to reply
 

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