当前位置:网站首页>501. 二叉搜索树中的众数
501. 二叉搜索树中的众数
2022-04-23 09:04:00 【yitahutu79】
给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。
如果树中有不止一个众数,可以按 任意顺序 返回。
假定 BST 满足如下定义:
结点左子树中所含节点的值 小于等于 当前节点的值
结点右子树中所含节点的值 大于等于 当前节点的值
左子树和右子树都是二叉搜索树
示例 1:

输入:root = [1,null,2,2]
输出:[2]
示例 2:
输入:root = [0]
输出:[0]
提示:
树中节点的数目在范围 [1, 104] 内
-105 <= Node.val <= 105
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
/** * Note: The returned array must be malloced, assume caller calls free(). */
struct TreeNode *pre;
int cnt, max_cnt;
void inorder(struct TreeNode *root, int *ret, int *pn) {
if (root == NULL) return;
inorder(root->left, ret, pn);
if (pre == NULL || pre->val != root->val) cnt = 1;
else cnt += 1;
pre = root;
if (cnt == max_cnt) {
ret[(*pn)++] = root->val;
} else if (cnt > max_cnt) {
max_cnt = cnt;
ret[0] = root->val;
*pn = 1;
}
inorder(root->right, ret, pn);
return;
}
int* findMode(struct TreeNode* root, int* returnSize){
int *ret = (int *)malloc(sizeof(int) * 10000);
*returnSize = 0;
pre = NULL;
cnt = 0;
max_cnt = -1;
inorder(root, ret, returnSize);
return ret;
}
版权声明
本文为[yitahutu79]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40713201/article/details/124357739
边栏推荐
- Pctp test experience sharing
- 深度学习框架中的自动微分及高阶导数
- MATLAB入门资料
- LGB, XGB, cat, k-fold cross validation
- Flink SQL realizes the integration of stream and batch
- Restore binary tree (25 points)
- Chris LATTNER, father of llvm: the golden age of compilers
- LLVM之父Chris Lattner:编译器的黄金时代
- 1099 建立二叉搜索树 (30 分)
- How does kubernetes use harbor to pull private images
猜你喜欢

Resource packaging dependency tree

Multi view depth estimation by fusing single view depth probability with multi view geometry

GoLand debug go use - white record

Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack

Brush classic topics

1099 establish binary search tree (30 points)

L2-022 rearrange linked list (25 points) (map + structure simulation)

Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)

I don't understand time, timestamp and time zone. Look at this article

Strength comparison vulnerability of PHP based on hash algorithm
随机推荐
Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
Go language self-study series | golang method
js 原型链的深入
I don't understand time, timestamp and time zone. Look at this article
ONEFLOW learning notes: from functor to opexprinter
Bk3633 specification
考研线性代数常见概念、问题总结
2021李宏毅机器学习之Adaptive Learning Rate
Restore binary tree (25 points)
L2-024 tribe (25 points) (and check the collection)
关于堆的判断 (25 分) 两种插入方式
L2-3 romantic silhouette (25 points)
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Harbor enterprise image management system
Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
Taxable income
Solidity 问题汇总
Failed to download esp32 program, prompting timeout
Cadence process angle simulation, Monte Carlo simulation, PSRR
2021 Li Hongyi's adaptive learning rate of machine learning