当前位置:网站首页>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);
}
}
边栏推荐
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- OpenVP*整合ldap認證
- 启牛的证券账户是真的吗?安全的吗?
- 样板小作坊
- MySQL table mechanism
- 在Oracle表中如何进行关键词搜索
- How to search keywords in Oracle tables
- Example analysis of SQL query optimization principle
- redis列表list常用命令大全
- Why is the kotlin language not popular now?
猜你喜欢

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

Fiddler configuration

Design language testing for functional testing: what tests are included in functional testing? What is the role of each

项目实训10——对特定数据库的备份

Wireshark抓取rtp负载ts流介绍(UDP组播)

一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue

300题 线代第一讲行列式

PwnTheBox,Pwn:tutorial1

Data and information resource sharing platform (6)

【视频】KMEANS均值聚类和层次聚类:R语言分析生活幸福指数可视化|数据分享
随机推荐
Im instant messaging source code with tutorial /uniapp instant messaging source code, with installation tutorial
R 语言绘制二维正态分布的密度曲面图;
Is it too late to teach yourself programming at 28? Is it reliable?
The time (in minutes) required for a group of workers to cooperate to complete the assembly process of a part are as follows:
Two debugging techniques in embedded software development
Why is the video fusion cloud service easycvr cluster video event query invalid?
Relevant knowledge of flowable BPMN
34. find the first and last positions of elements in the sorted array - binary search, double pointer
Commonplace - the timetable of the great philosopher Kant
Halcon combined with C # to detect surface defects -- affine transformation (II)
Yuntu says that every successful business system cannot be separated from apig
SQL查询优化原理实例分析
Fiddler configuration
关于嵌入式音频开发需要注意的两个方面
30 frequently asked questions of 2022 latest software test interview questions [continuous update ~]
Prefer "big and small weeks", only write 200 lines of code every day, and the monthly salary of 8k-17k people will rise again
Unity code registers events for animation
06151020 mysterious code, waiting for you to decipher
字蛛(font-spider)教学——ttf/otf字体文件压缩
考研英语词汇 unit1