当前位置:网站首页>leetcode刷题:二叉树23(二叉搜索树中的众数)
leetcode刷题:二叉树23(二叉搜索树中的众数)
2022-07-07 10:30:00 【涛涛英语学不进去】
501.二叉搜索树中的众数
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
假定 BST 有如下定义:
- 结点左子树中所含结点的值小于等于当前结点的值
- 结点右子树中所含结点的值大于等于当前结点的值
- 左子树和右子树都是二叉搜索树
例如:
给定 BST [1,null,2,2],
返回[2].
提示:如果众数超过1个,不需考虑输出顺序
进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
两种思路:
1: 通用写法,哈希表,遍历树,把结果集存到hashmap,遍历频率value获取最大值,再遍历一遍获取结果集,处理成数组。
HashMap<Integer, Integer> result = new HashMap<Integer, Integer>();
public int[] findMode(TreeNode root) {
//本题可以使用哈希表做
traversal(root);
Set<Integer> keys = result.keySet();
int max = -1;
for (Integer key : keys) {
if (result.get(key) > max) {
max = result.get(key);
}
}
Set<Integer> resultSet = new HashSet<Integer>();
for (Integer key : keys) {
if (result.get(key) == max) {
resultSet.add(key);
}
}
int[] resultArr = new int[resultSet.size()];
int index = 0;
for (Integer key : resultSet) {
resultArr[index] = key;
index++;
}
return resultArr;
}
public void traversal(TreeNode root) {
if (root == null) {
return;
}
traversal(root.left);
if (result.containsKey(root.val)) {
result.put(root.val, result.get(root.val) + 1);
} else {
result.put(root.val, 1);
}
traversal(root.right);
}
2.二叉搜索树写法:因为是二叉搜索树,非递减,所以一样的数肯定都是在一块的,计算每个数出现的次数,给一个最大出现次数,如果当前数出现次数比最大出现次数小,不做改变,若相等,则把当前数添加到结果集,如果比最大出现次数大,则清空结果集,当前数添加到结果集。
List<Integer> list = new ArrayList<>();
TreeNode parent;
int maxTimes = 0;
int curTimes = 0;
public void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
inorderTraversal(root.left);
if (root.val == parent.val) {
curTimes++;
//如果当前元素出现次数比最大次数大,则新的众数出现,原来的不是众数了
if (curTimes > maxTimes) {
maxTimes = curTimes;
list.clear();
list.add(root.val);
} else if (curTimes == maxTimes) {
//如果当前元素出现次数和最大次数相等,则新的众数出现
list.add(root.val);
}
} else {
curTimes = 1;
if (curTimes > maxTimes) {
maxTimes = curTimes;
list.clear();
list.add(root.val);
} else if (curTimes == maxTimes) {
//如果当前元素出现次数和最大次数相等,则新的众数出现
list.add(root.val);
}
parent = root;
}
inorderTraversal(root.right);
}
public int[] findMode(TreeNode root) {
parent = root;
inorderTraversal(root);
int index = 0;
int[] resultArr = new int[list.size()];
for (Integer result : list) {
resultArr[index] = result;
index++;
}
return resultArr;
}
边栏推荐
- 开发一个小程序商城需要多少钱?
- How much does it cost to develop a small program mall?
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
- 免备案服务器会影响网站排名和权重吗?
- <No. 9> 1805. Number of different integers in the string (simple)
- Customize the web service configuration file
- SQL blind injection (WEB penetration)
- Tutorial on the principle and application of database system (008) -- exercises on database related concepts
- 【统计学习方法】学习笔记——第五章:决策树
- Learning and using vscode
猜你喜欢
问题:先后键入字符串和字符,结果发生冲突
Introduction and application of smoothstep in unity: optimization of dissolution effect
数据库系统原理与应用教程(011)—— 关系数据库
BGP third experiment report
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
Vxlan static centralized gateway
<No. 8> 1816. 截断句子 (简单)
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
随机推荐
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
Vxlan static centralized gateway
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
What is a LAN domain name? How to parse?
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
什么是局域网域名?如何解析?
Several methods of checking JS to judge empty objects
SQL Lab (46~53) (continuous update later) order by injection
Attack and defense world ----- summary of web knowledge points
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
About web content security policy directive some test cases specified through meta elements
静态Vxlan 配置
Zero shot, one shot and few shot
数据库系统原理与应用教程(009)—— 概念模型与数据模型
ES底层原理之倒排索引
Decrypt gd32 MCU product family, how to choose the development board?
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
Cenos openssh upgrade to version 8.4
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
利用栈来实现二进制转化为十进制