当前位置:网站首页>LeetCode 501 :二叉搜索树中的众数
LeetCode 501 :二叉搜索树中的众数
2022-06-10 22:21:00 【斯沃福德】
题目:

思路:
使用pre节点和root节点进行比较
用count来记录节点值重复的次数
①先确定count值才能确定是否添加root值
若pre为null,或者pre和root值不等时 则开始记数;如果pre和root等则count++累加
②用count和max比较,如果count比max大,则之前list存的都无效,清除list
若count=max,即有新的众数出现,添加至list
若count<max,忽略
class Solution {
int count=0;
int max=Integer.MIN_VALUE;
TreeNode pre=null;
ArrayList<Integer> list;
public int[] findMode(TreeNode root) {
list=new ArrayList<>();
check(root);
int n=list.size();
int[]r =new int[n];
for(int i=0;i<n;i++){
r[i]=list.get(i);
}
return r;
}
void check(TreeNode root){
if(root==null){
return;
}
check(root.left);
//中序遍历
//开始记数,重新记数
if(pre==null || pre.val!=root.val){
count=1;
}else if(root.val==pre.val){
count++;
}
//存入结果
if(count>max){
// 若有重复的则只存count最高那个节点的值!
list.clear();
list.add(root.val);
max=count;
}else if(count==max){
list.add(root.val);
}
//count<max则不存
pre=root;
check(root.right);
}
}
边栏推荐
- Why is the video fusion cloud service easycvr cluster video event query invalid?
- Openvp * consolider l'authentification LDAP
- Project training 10 - backup of specific databases
- PwnTheBox,Web:hello
- 集度夏一平:不是所有事都向李彦宏汇报,靠产品跟小米华为竞争
- 数据文件 Insurance csv包含1338条观测,即目前已经登记过的保险计划受益者以及表示病人特点和历年计划入的总的医疗费用的特征。这些特征是
- 数据与信息资源共享平台(四)
- LeetCode+ 16 - 20
- 1. open the R program, and use the apply function to calculate the sum of 1 to 12 in the sequence of every 3 numbers. That is, calculate 1+4+7+10=? 2+5+8+11=?, 3+6+9+12=?
- Laravel8 enables alicloud file upload
猜你喜欢
![The shell script of pagoda plan task regularly deletes all files under a directory [record]](/img/dc/cfd449bf14c4545cd2e52bda4ab31e.png)
The shell script of pagoda plan task regularly deletes all files under a directory [record]

LeetCode+ 21 - 25

第六章——分枝限界法

The time (in minutes) required for a group of workers to cooperate to complete the assembly process of a part are as follows:

Dell R730 raid5 安装Server 2016(解决磁盘大于2T)

数据与信息资源共享平台(五)

Executor - Shutdown、ShutdownNow、awaitTermination 詳解與實戰

Is it too late to teach yourself programming at 28? Is it reliable?

Project training 11 - regular backup of database

Data and information resource sharing platform (VII)
随机推荐
Halcon combined with C # to detect surface defects -- affine transformation (II)
Chapter 6 - branch and bound method
Introduction to Wireshark capturing RTP load TS stream (UDP multicast)
开中银证券账户安全吗?风险高吗?
乘风破浪,探索数据可视化开发平台 FlyFish 开源背后的秘密!
Unity code registers events for animation
Data and information resource sharing platform (VIII)
Laravel8 enables alicloud file upload
What are the common methods of object
Gather for summer Yiping: not everything is reported to Robin Lee. They compete with Xiaomi Huawei by products
Commonplace - the timetable of the great philosopher Kant
Simple impedance matching circuit and formula
Dell r730 RAID5 installation server 2016 (disk larger than 2t)
Openvp* integrated LDAP authentication
Dependencymanagement and dependencies
Exécuteur - shutdown, shutdown Now, awaittermination details and actual Fighting
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
一 组工人合作完成某一部件的装配工序所需的时间(单位:分钟)分别如下:
关于idea中src下 无法new一个package
[Interface tutorial] how does easycvr set platform cascading through the interface?