当前位置:网站首页>1115 counting nodes in a BST (30 points)
1115 counting nodes in a BST (30 points)
2022-07-03 04:55:00 【vs5】
The main idea of the topic : According to the given sequence , Build a binary search tree , Find the number of nodes in the last two layers
Insert node tree , Search wide or deep to find the number of nodes in each layer
#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;
int n,x,f;
unordered_map<int,int>mp;
struct node
{
int v;
node *left,*right;
node(int x): v(x),left(NULL),right(NULL){}
}*tr;
node *insert(node *t,int val)
{
if(t == NULL) t = new node(val);
else if(t -> v < val) t -> right = insert(t -> right,val);
else t -> left = insert(t -> left,val);
return t;
}
void bfs(node *tr)
{
queue<pair<node*,int>>que;
que.push({tr,0});
while(que.size())
{
auto t = que.front();que.pop();
auto u = t.first;f = t.second;
mp[f] ++;
if(u -> left != NULL) que.push({u -> left,f + 1});
if(u -> right != NULL) que.push({u -> right,f + 1});
}
}
int main()
{
cin >> n;
for(int i = 0; i < n; i ++)
{
cin >> x;
tr = insert(tr,x);
}
bfs(tr);
int d1 = mp[f],d2 = mp[f - 1];
printf("%d + %d = %d",d1,d2,d1 + d2);
return 0;
}边栏推荐
- I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
- Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
- Introduction to JVM principle
- Learn to use the idea breakpoint debugging tool
- Analysis of proxy usage of ES6 new feature
- Kept hot standby and haproxy
- Review the old and know the new: Notes on Data Science
- Number of 1 in binary (simple difficulty)
- Market status and development prospect prediction of the global fire extinguisher industry in 2022
- Shell script -- condition judgment
猜你喜欢

Triangular rasterization

Kept hot standby and haproxy

Pyqt control part (II)

2022-02-12 daily clock in: problem fine brush

On typescript and grammar

LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)

Apache MPM model and ab stress test

Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)

5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip

Analysis of proxy usage of ES6 new feature
随机推荐
Valentine's day limited withdrawal guide: for one in 200 million of you
Market status and development prospects of the global IOT active infrared sensor industry in 2022
[BMZCTF-pwn] 20-secret_ file
【SQL注入点】注入点出现位置、判断
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
Network security textual research recommendation
Review the old and know the new: Notes on Data Science
Leetcode simple question: the key with the longest key duration
[develop wechat applet local storage with uni app]
Retirement plan fails, 64 year old programmer starts work again
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
论文阅读_中文NLP_ELECTRA
移动端——uniapp开发记录(公共请求request封装)
Shuttle + Alluxio 加速内存Shuffle起飞
stm32逆向入门
The reason why the entity class in the database is changed into hump naming
MySQL winter vacation self-study 2022 12 (3)
雇佣收银员(差分约束)
"Niuke brush Verilog" part II Verilog advanced challenge
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)