当前位置:网站首页>Leetcode 501: mode in binary search tree
Leetcode 501: mode in binary search tree
2022-06-10 23:35:00 【Swarford】
The mode in the binary search tree
subject :

Ideas :
Use pre Nodes and root Nodes
use count To record the number of times the node value is repeated
① Make sure the count Value to determine whether to add root value
if pre by null, perhaps pre and root When the values are not equal Start counting ; If pre and root Equivalent rule count++ Add up
② use count and max Compare , If count Than max Big , Then before list None of them are valid , eliminate list
if count=max, That is, a new mode appears , Added to the list
if count<max, Ignore
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);
// In the sequence traversal
// Start counting , Recount
if(pre==null || pre.val!=root.val){
count=1;
}else if(root.val==pre.val){
count++;
}
// Deposit results
if(count>max){
// If there are duplicates, only count The value of the highest node !
list.clear();
list.add(root.val);
max=count;
}else if(count==max){
list.add(root.val);
}
//count<max Then it doesn't exist
pre=root;
check(root.right);
}
}
边栏推荐
- Is it too late to teach yourself programming at 28? Is it reliable?
- 宝塔计划任务Shell脚本定时删除某各目录下所有文件【记录】
- Data and information resource sharing platform (VII)
- Sealem Finance - a new decentralized financial platform based on Web3
- Two aspects of embedded audio development
- Gather for summer Yiping: not everything is reported to Robin Lee. They compete with Xiaomi Huawei by products
- MySQL mvcc multi version concurrency control
- Dell r730 RAID5 installation server 2016 (disk larger than 2t)
- 上海股票开户是安全的吗?
- Build TAR model using beersales data set in TSA package
猜你喜欢

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

项目实训11——对数据库的定时备份

Six procurement challenges perplexing Enterprises

Development and implementation of AI intelligent video analysis easycvr platform device channel batch deletion function

300题 线代第一讲行列式

Kotlin语言现在怎么不火了?

Lenovo explained for the first time the five advantages of hybrid cloud Lenovo xcloud and how to build an intelligent digital base

Gather for summer Yiping: not everything is reported to Robin Lee. They compete with Xiaomi Huawei by products

2022最新软件测试面试题常问的30道【持续更新~】

已知某种木材的横纹抗压力服从N(x,d2),现对十个试件作横纹抗压力试验,得数据如下:(单位:kg/cm2)
随机推荐
[QPSK if] Verilog design of QPSK IF signal generation module based on FPGA
PwnTheBox,Pwn:tutorial1
LeetCode+ 16 - 20
Sealem Finance - a new decentralized financial platform based on Web3
Gather for summer Yiping: not everything is reported to Robin Lee. They compete with Xiaomi Huawei by products
30 frequently asked questions of 2022 latest software test interview questions [continuous update ~]
项目实训13——界面补充
启牛的证券是哪个证券公司的呢?安全吗?
Clustered and non clustered indexes
Is it safe to open an account in Shanghai Stock Exchange?
Before we learn about high-performance computing, let's take a look at its history
ILRuntime热更框架 安装以及断点调试
关于嵌入式音频开发需要注意的两个方面
0223 summary
样板小作坊
SQL查询优化原理实例分析
Development and implementation of AI intelligent video analysis easycvr platform device channel batch deletion function
【sql语句基础】——增(insert)
1.打开R程序,使用 apply 函数计算1到12按先后次序每隔3个数一组的和。即,计算1+4+7+10=?2+5+8+11=?,3+6+9+12=?
Optimize code to remove if else