首页 > 其他 > 详细

【leetcode】二叉搜索树中的众数

时间:2020-09-29 19:46:20      阅读:38      评论:0      收藏:0      [点我收藏+]

 

int* answer;
int answerSize;
int base, count, maxCount;

void update(int x) {
    if (x == base) {
        ++count;
    } else {
        count = 1;
        base = x;
    }
    if (count == maxCount) {
        answer[answerSize++] = base;
    }
    if (count > maxCount) {
        maxCount = count;
        answerSize = 0;
        answer[answerSize++] = base;
    }
}

void dfs(struct TreeNode* o) {
    if (!o) {
        return;
    }
    dfs(o->left);
    update(o->val);
    dfs(o->right);
}

int* findMode(struct TreeNode* root, int* returnSize) {
    base = count = maxCount = 0;
    answer = malloc(sizeof(int) * 4001);
    answerSize = 0;
    dfs(root);
    *returnSize = answerSize;
    return answer;
}

 

【leetcode】二叉搜索树中的众数

原文:https://www.cnblogs.com/ganxiang/p/13751085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!