当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Threejs Internet of things, 3D visualization of farms (I)
Managed service network: application architecture evolution in the cloud native Era
Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt
蛇形矩阵
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
网络安全-记录web漏洞修复
托管式服务网络:云原生时代的应用体系架构进化
Sword finger offer 04 Search in two-dimensional array
自动语音识别(ASR)研究综述
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
随机推荐
Live broadcast preview | container service ack elasticity prediction best practice
机器学习 --- 决策树
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Bit operation skills
概率论与数理统计考试重点复习路线
Network security - record web vulnerability fixes
[phantom engine UE] package error appears! Solutions to findpin errors
Hexadecimal to decimal
FFmepg使用指南
How can CIOs use business analysis to build business value?
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
Practice | mobile end practice
Debug insights
Threejs Internet of things, 3D visualization of farms (II)
指针函数(基础)
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
PR video clip (project packaging)
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
电源管理总线 (PMBus)
Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt