my ugly codes........


  • 0
    S

    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • struct TreeNode *left;
      
    • struct TreeNode *right;
      
    • };
      /
      /
      *
    • Return an array of size *returnSize.
    • Note: The returned array must be malloced, assume caller calls free().
      */
      int last; //队列中的前一个数
      int flag;
      int cnt;
      int max_cnt = 0;
      int max_num;
      int i;
      int *a;
      int capacity;

    void visit_in_order(struct TreeNode* root)
    {
    if(root == NULL)
    return ;
    visit_in_order(root->left);
    if(flag == 0)
    {
    last = root->val; // a[0]
    flag ++;
    cnt = 1;
    }
    else
    {
    if(last == root->val) // a[i] == a[i-1]
    {
    cnt ++;
    }
    else
    {
    cnt = 1; //重新计数
    }
    last = root->val;
    }
    if(cnt>max_cnt)
    {
    max_cnt = cnt;
    a[0] = last;
    i = 1;
    }
    else if(cnt == max_cnt)
    {
    if(i==capacity-1)
    {
    int tmp = (int)malloc((capacity+100)sizeof(int));
    memcpy(tmp,a,capacity
    sizeof(int));
    free(a);
    a = tmp;
    capacity+=100;
    }
    a[i++] = last;
    }

    visit_in_order(root->right);
    

    }

    int* findMode(struct TreeNode* root, int* returnSize) {
    int cnt = 0;
    int tmp;

    a = (int*)malloc(100*sizeof(int));
    capacity = 100;
    flag = 0;
    cnt = 0;
    max_cnt = 0;
    i = 0;
    
    visit_in_order(root);
    
    
    *returnSize = i;
    
    return a;
    

    }


Log in to reply
 

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