当前位置:网站首页>11. < tag binary tree and BST foundation > lt.501 Mode in binary search tree
11. < tag binary tree and BST foundation > lt.501 Mode in binary search tree
2022-06-09 12:01:00 【Caicai's big data development path】
lt.501. The mode in the binary search tree
[ Case needs ]

[ Train of thought analysis 1 , Violence solution ]
- We can not use BST Characteristics of , Think of it as an ordinary binary tree ,
- The most intuitive solution is to traverse the whole tree , Record all the nodes and frequencies encountered to map In the middle ,
- namely <Integer, Integer> = < The value of a binary tree node , Frequency of occurrence >
- In addition to map At the same time , We can find the maximum frequency of each node .
The maximum frequency , In us dfs After that , It can be used map.keySet() Ergodic time ,
utilize get(key) == Maximum frequency , Find the number of nodes corresponding to the maximum frequency , Deposit in list Then you can
And then list Convert to array and return ;
[ Code implementation ]
//1. Violence solution
/** * 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) {
// The most frequent element
// In the sequence traversal BST It's an ascending sequence
//1. violence , use HashMap Record root.val And the number of times <root.val, frequency >
Map<Integer, Integer> map = new HashMap<>();
//2. Get the frequency map
dfs(map, root);
//3. Traverse to the maximum frequency and store it in 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);
// }
// The purpose is to obtain map in freq maximal , If there is an equal maximum , Deposit together
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. Recursive export
if(root == null)return;
//3. Single layer recursive logic
dfs(map, root.left);
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
// Get the maximum frequency
maxFreq = Math.max(maxFreq, map.getOrDefault(root.val, 0));
dfs(map, root.right);
}
}
[ Train of thought analysis II , DFS, utilize BST nature ]
- Remember this sentence : Yes BST In the sequence traversal , What you get is an ascending sequence
- For this question , We use the middle order traversal to get the ascending sequence , Then the repeated numbers are continuous , We just need to record the number corresponding to the maximum number of repetitions .
- Detailed explanation : Am I
[ Code implementation ]
/** * 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;
}
// In the sequence traversal BST, Compare the previous node each time ,
//1. Same record to count, Deposit... At a different time next time list;
//2.
public void dfs(List<Integer> list, TreeNode root){
//1. Recursion end condition
if(root == null)return;
dfs(list, root.left);
//1. Record the possible modes
if(pre == null || pre.val == root.val){
++count;
}else{
count = 1;
}
//2. Add a large mode to list,
// If you meet a bigger , Empty list, To add
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);
}
}
边栏推荐
- 8.搜索插入位置
- 你的微服务敢独立交付么?
- 系统集成项目管理工程师考试说明
- Preparation guide for 2022 soft test information security engineer examination
- Preparation guide for 2022 soft test senior network planning designer
- 3.<tag-回溯和组合及其剪枝>lt.17. 电话号码的字母组合
- 企评家用杜邦分析法剖析:华东建筑集团股份有限公司企业财务状况
- [转载] 分布式系统的“脑裂”到底是个什么玩意?
- 【直播回顾】Hello HarmonyOS应用篇第六课——短视频应用开发
- Win10 20H2正式发布,对比旧版&新功能一览
猜你喜欢
![[untitled]](/img/c1/35479e8bd3832e3b6d9452768c4f13.png)
[untitled]

11.<tag-二叉树和BST基础>lt.501. 二叉搜索树中的众数

Win10 20H2正式发布,对比旧版&新功能一览

04 | 万事预则立:中台建设前必须想清楚的四个问题

06 | the first step of China Taiwan landing: enterprise strategy decomposition and current situation research (Discovery)

给 Web3 项目的智能合约安全指南

由于没有远程桌面授权服务器可以提供许可证

从2022年的这几篇论文看推荐系统序列建模的趋势

09 | the fourth step: the construction and delivery of the middle office

8K resolution 7680*4320
随机推荐
清空表格选中项clearSelection
15 Win32 class library name in WMI
2021年下半年系统集成项目管理工程师案例分析真题及答案解析
Real questions and answers of comprehensive knowledge of system integration project management engineer in the second half of 2021
【补丁分析】CVE-2016-8610:对导致拒绝服务的“SSL Death Alert”漏洞补丁分析
H3C认证无线高级工程师
Go zero micro Service Practice Series (II. Service splitting)
工具类记录之Guawa的Splitter
H3C认证网络工程师
OpenSSL 安全漏洞(CVE-2016-8610)修复详情步骤
Apple claims that M2 is 26 times stronger than Intel i5. The truth of false marketing has been revealed!
06 | 中台落地第一步:企业战略分解及现状调研(Discovery)
2022年软考信息安全工程师考试备考指南
接力AlphaFold!星药科技重磅发布TBind-开启分子蛋白复合物结构预测新纪元
U8g2 graphics library and STM32 migration (I2C, software and hardware)
Win10 20H2正式发布,对比旧版&新功能一览
H3C认证路由交换网络高级工程师
H3C Certified Network Engineer
传入base64集合,导出大图片
09 | the fourth step: the construction and delivery of the middle office