当前位置:网站首页>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;
}

边栏推荐
- SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
- Tutorial on principles and applications of database system (009) -- conceptual model and data model
- ES底层原理之倒排索引
- Completion report of communication software development and Application
- 静态Vxlan 配置
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- Tutorial on principles and applications of database system (007) -- related concepts of database
- 防红域名生成的3种方法介绍
- 问题:先后键入字符串和字符,结果发生冲突
- DOM parsing XML error: content is not allowed in Prolog
猜你喜欢

EPP+DIS学习之路(2)——Blink!闪烁!

Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios

<No. 8> 1816. Truncate sentences (simple)

Upgrade from a tool to a solution, and the new site with praise points to new value

BGP actual network configuration

SQL Lab (41~45) (continuous update later)

静态Vxlan 配置

@What happens if bean and @component are used on the same class?

PowerShell cs-utf-16le code goes online

千人规模互联网公司研发效能成功之路
随机推荐
EPP+DIS学习之路(2)——Blink!闪烁!
Apache installation problem: configure: error: APR not found Please read the documentation
File upload vulnerability - upload labs (1~2)
AirServer自动接收多画面投屏或者跨设备投屏
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
Epp+dis learning path (1) -- Hello world!
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
【PyTorch实战】用RNN写诗
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
静态Vxlan 配置
小红书微服务框架及治理等云原生业务架构演进案例
Inverted index of ES underlying principle
密码学系列之:在线证书状态协议OCSP详解
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
Utiliser la pile pour convertir le binaire en décimal
About sqli lab less-15 using or instead of and parsing
跨域问题解决方案
EPP+DIS学习之路(1)——Hello world!
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer