当前位置:网站首页>11.<tag-二叉树和BST基础>lt.501. 二叉搜索树中的众数
11.<tag-二叉树和BST基础>lt.501. 二叉搜索树中的众数
2022-06-09 11:15:00 【菜菜的大数据开发之路】
lt.501. 二叉搜索树中的众数
[案例需求]

[思路分析一, 暴力解法]
- 我们可以不利用BST的特性, 把它看作是一颗普通的二叉树,
- 最直观的解法就是遍历整棵树, 把遇到的节点和频度全部记录到map中去,
- 即 <Integer, Integer> = <二叉树节点的值, 出现的频度>
- 在添加到map的同时, 我们可以找出每个节点对应频度的最大值.
这个频度最大值, 在我们dfs结束之后, 可以在用map.keySet() 遍历时,
利用get(key) == 最大频度, 找出最大频率对应的节点数, 存入list中即可
再把list转换为数组返回即可;
[代码实现]
//1. 暴力解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
int maxFreq = 0;
public int[] findMode(TreeNode root) {
// 出现频率最高的元素
// 中序遍历BST是个升序的序列
//1. 暴力, 用HashMap记录root.val和出现的次数 <root.val, 次数>
Map<Integer, Integer> map = new HashMap<>();
//2. 获得频率map
dfs(map, root);
//3. 遍历到最大的频率存入到list
//int count = 1;
List<Integer> list = new ArrayList<>();
for(int key : map.keySet()){
// if(count < map.get(key)){
// list.add(key);
// }else if(list.size() > 0 && count == map.get(key)
// && key != list.get(list.size() - 1)){
// list.add(key);
// }
//目的是为了获取到map中freq最大的, 如果存在相等的最大的, 一起存入即可
if(map.get(key) == maxFreq){
list.add(key);
}
}
int[] res = new int[list.size()];
int index = 0;
for(int x : list){
res[index++] = x;
}
return res;
}
public void dfs(Map<Integer, Integer> map, TreeNode root){
//2. 递归出口
if(root == null)return;
//3. 单层递归逻辑
dfs(map, root.left);
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
//获取到最大的频率
maxFreq = Math.max(maxFreq, map.getOrDefault(root.val, 0));
dfs(map, root.right);
}
}
[思路分析二, DFS, 利用BST性质]
- 铭记这句话: 对BST中序遍历, 得到的是一个升序的序列
- 对于本题, 我们利用中序遍历得到升序的序列, 那么重复的数字都是连续的, 我们只需记录下最大的重复次数所对应的数即可。
- 详细题解: 点我
[代码实现]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
int maxFreq = 0;
int count = 0;
TreeNode pre = null;
public int[] findMode(TreeNode root) {
List<Integer> list = new ArrayList<>();
dfs(list, root);
int[] res = new int[list.size()];
int index = 0;
for(int x : list){
res[index++] = x;
}
return res;
}
//中序遍历BST, 每次比较前一个节点,
//1. 相同记录到count, 在下次不相同时存入list;
//2.
public void dfs(List<Integer> list, TreeNode root){
//1. 递归结束条件
if(root == null)return;
dfs(list, root.left);
//1. 记录可能的众数
if(pre == null || pre.val == root.val){
++count;
}else{
count = 1;
}
//2. 将大的众数添加到list,
// 如果遇见更大的, 清空list, 重新添加
if(count == maxFreq){
list.add(root.val);
}else if(count > maxFreq){
maxFreq = count;
list.clear();
list.add(root.val);
}
pre = root;
dfs(list,root.right);
}
}
边栏推荐
- HEVC之HM学习02
- How to solve the existing 1px problem?
- H3C认证云计算工程师
- H3C Certified Wireless senior engineer
- How to make money through Hongmeng ecology?
- 工具类记录之Guawa的Splitter
- 03 | definition of China Taiwan: what are we talking about when we talk about China Taiwan?
- 6.<tag-回溯和切割问题>lt.131.分割回文串
- No remote desktop license server can provide licenses
- R语言caTools包进行数据划分、randomForest包构建随机森林模型、使用plot函数可视化训练好的随机森林模型的过程(随着树的个数的增加误差越来越稳定)
猜你喜欢

Chapter III transport layer

9.<tag-回溯和同层内去重的组合问题>lt.491. 递增子序列

How to solve the existing 1px problem?

使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
![[signalr complete series] Realization of signalr real-time communication in net core](/img/af/4b8bfea6238c646c54352635d5da98.png)
[signalr complete series] Realization of signalr real-time communication in net core

How to model 3DMAX (I)

8.<tag-回溯和全排列>lt.46. 全排列 + lt.47. 全排列 II

tag贪心-刷题预备知识-贪心解题方法 + lt.455. 分发饼干 + lt.376. 摆动序列

详解PCB线路板覆铜基础知识

Daily question -leetcode1037- effective boomerang
随机推荐
8.<tag-回溯和全排列>lt.46. 全排列 + lt.47. 全排列 II
Computer quick index query software listary
Windows远程时提示CredSSP加密数据库修正
8K resolution 7680*4320
Word中表格如何均匀铺满整页
H3C Certified Safety Technology Senior Engineer
【转载】搞懂G1垃圾收集器
由于没有远程桌面授权服务器可以提供许可证
R语言测试构建好的决策树模型(基于测试数据集)、进行预测推理并使用table函数计算混淆矩阵、基于混淆矩阵计算模型准确度accuray
06 | 中台落地第一步:企业战略分解及现状调研(Discovery)
R语言caTools包进行数据划分、randomForest包构建随机森林模型、并查看模型输出的基本信息(树的个数、OOB包外估计误差、混淆矩阵、每个类别误差等)
Security evaluation of commercial password application
小米智能摄像机云台Pro如何插入视频监控存储卡
What are the steps and methods for PCB debugging?
9.<tag-回溯和同层内去重的组合问题>lt.491. 递增子序列
H3C Certified Wireless Internet expert
Chapter III transport layer
使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
tag贪心-刷题预备知识-贪心解题方法 + lt.455. 分发饼干 + lt.376. 摆动序列
Learning notes of segmentation, paging, page table and quick table