A simple C solution[Accepted]


  • 0
    B
    void order(struct TreeNode* root,int *count,int a[2][10000],int *max)
    {
        if(root==NULL)
        {return ;}
        order(root->left,count,a,max);
        if(a[1][*count]==0)
        {
            a[0][*count]=root->val;
            a[1][*count]++;
        }else if(root->val!=a[0][*count])
        {
            (*count)++;
            a[0][*count]=root->val;
            a[1][*count]++;
        }else
        {a[1][*count]++;}
        if(*max<a[1][*count])
        {*max=a[1][*count];}
        order(root->right,count,a,max);
    }
    int* findMode(struct TreeNode* root, int* returnSize) {
        if(root==NULL)
        {return NULL;}
        int a[2][10000]={0};
        int *max=(int*)malloc(sizeof(int));
        *max=0;
        int *count=(int*)malloc(sizeof(int));
        *count=0;
        order(root,count,a,max);
        int *result=(int*)malloc(sizeof(int)*10000);
        *returnSize=0;
        for(int i=0;i<=*count;i++)
        {
            if(a[1][i]==*max)
            {result[(*returnSize)++]=a[0][i];}
        }
        return result;
    }
    

Log in to reply
 

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