当前位置:网站首页>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;
}
边栏推荐
- Matplotlib draws three-dimensional scatter and surface graphs
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- Serpentine matrix
- 蛇形矩阵
- NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
- [finebi] the process of making custom maps using finebi
- Leetcode hot topic Hot 100 day 33: "subset"
- C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
- Seven join join queries of MySQL
- Managed service network: application architecture evolution in the cloud native Era
猜你喜欢
[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)
揭秘技术 Leader 必备的七大清奇脑回路
How to get the first few pieces of data of each group gracefully
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Reading and visualization of DICOM, MHD and raw files in medical imaging
Function (basic: parameter, return value)
Managed service network: application architecture evolution in the cloud native Era
Uncover the seven quirky brain circuits necessary for technology leaders
【虚幻引擎UE】运行和启动的区别,常见问题分析
2022-2028 global and Chinese FPGA prototype system Market Research Report
随机推荐
直播預告 | 容器服務 ACK 彈性預測最佳實踐
level18
蛇形矩阵
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
网络安全-记录web漏洞修复
这是一个不确定的时代
函數(易錯)
Fuel consumption calculator
Serpentine matrix
Reading and visualization of DICOM, MHD and raw files in medical imaging
[uniapp] system hot update implementation ideas
Decimal to hexadecimal
Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
How can CIOs use business analysis to build business value?
OWASP top 10 vulnerability Guide (2021)
Mxnet imports various libcudarts * so、 libcuda*. So not found
取余操作是一个哈希函数
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
Cookie learning diary 1
机器学习 --- 决策树