当前位置:网站首页>Mode in BST (binary tree & Notes on question brushing)
Mode in BST (binary tree & Notes on question brushing)
2022-07-05 04:32:00 【Toolman firefly】
Violence statistics . Common tree practice , Roll it first , Let's look at the specific frequency
- map: The internal is RB Trees , The output is orderly .
- unordered_map yes hash function , Output disorder
private:
unordered_map<int,int> mp;
public:
void dfs(TreeNode* cur_node)
{
if(cur_node==nullptr) return ;
mp[cur_node->val]++;
dfs(cur_node->left);
dfs(cur_node->right);
}
bool static cmp(const pair<int,int>&a,const pair<int,int>&b)
{
return a.second>b.second;
}
vector<int> FindNode(TreeNode* root)
{
vector<int> res;
if(root==nullptr) return ;
dfs(root);
vector<pair<int,int>> vec(mp.begin(),mp.end());/* Very important */
sort(vec.begin(),vec.end(),cmp);/* Note the return value of the function */
res.push_back(vec[0].first);
for(int i=1;i<vec.size();i++)
{
if(vec[i].second==vec[0].second)
res.push_back(vec[i].first);
else
break;
}
return res;
}
Because of the mention of BST We're going to use BST Characteristics of ,BST Middle order traversal of , Is an ascending array ‘
private:
int count;
int max_count;
TreeNode* pre_node;
vector<int> res;
public:
void dfs(TreeNode* cur_node);
{
if(cur_node==nullptr) return;
dfs(cur_node->left);/*================================ Left */
if(pre_node==nullptr) count=1;/* First node */
else if(pre_node->val==cur_node->val) count++;/* The same value as the previous node */
else count=1;/* Different from the previous node value */
pre_node=cur_node;/* Update is a node */
if(count==max_count) res.push_back(cur->val);
if(count>max_count) {
max_count=count;
res.clear();
res.push_baxk(cur_node->val);
}
dfs(cur_node->right);/*============================== Right */
}
vector<int> FindNode(TreeNode* root)
{
count=0;
max_count=0;
pre_node=nullptr;
res={
};
dfs(root);
return res;
}
边栏推荐
- NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
- Reading and visualization of DICOM, MHD and raw files in medical imaging
- 线上故障突突突?如何紧急诊断、排查与恢复
- This is an age of uncertainty
- Discussion on the dimension of confrontation subspace
- [phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
- 2022-2028 global and Chinese video coding and transcoding Market Research Report
- Introduction to RT thread kernel (4) -- clock management
- Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
- Ctfshow 2022 Spring Festival welcome (detailed commentary)
猜你喜欢
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
User behavior collection platform
Cookie learning diary 1
TPG x AIDU|AI领军人才招募计划进行中!
MacBook installation postgresql+postgis
取余操作是一个哈希函数
windows下Redis-cluster集群搭建
【虚幻引擎UE】运行和启动的区别,常见问题分析
【科普】热设计基础知识:5G光器件之散热分析
解密函数计算异步任务能力之「任务的状态及生命周期管理」
随机推荐
A survey of automatic speech recognition (ASR) research
Scope of package class package
MacBook安装postgreSQL+postgis
windows下Redis-cluster集群搭建
自动语音识别(ASR)研究综述
Cookie learning diary 1
【虚幻引擎UE】实现测绘三脚架展开动画制作
SPI read / write flash principle + complete code
Invalid bound statement (not found) in idea -- problem solving
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
直播预告 | 容器服务 ACK 弹性预测最佳实践
Realize the attention function of the article in the applet
Hexadecimal to octal
Serpentine matrix
Label exchange experiment
2022-2028 global and Chinese FPGA prototype system Market Research Report
Key review route of probability theory and mathematical statistics examination
Threejs Internet of things, 3D visualization of farms (I)
Decimal to hexadecimal
Study notes 7