当前位置:网站首页>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;
}
边栏推荐
- Notes | numpy-11 Array operation
- 【SQL注入】联合查询(最简单的注入方法)
- Triangular rasterization
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- MySQL winter vacation self-study 2022 12 (3)
- Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
- [research materials] 2021 China's game industry brand report - Download attached
- 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
- Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
- 2022-02-12 daily clock in: problem fine brush
猜你喜欢
Interface frequency limit access
Retirement plan fails, 64 year old programmer starts work again
Keepalived热备与HAProxy
【XSS绕过-防护策略】理解防护策略,更好的绕过
Introduction to message queuing (MQ)
逆袭大学生的职业规划
并发操作-内存交互操作
Compile and decompile GCC common instructions
Career planning of counter attacking College Students
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
随机推荐
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
[tools run SQL blind note]
Retirement plan fails, 64 year old programmer starts work again
Market status and development prospect forecast of global heat curing adhesive industry in 2022
String matching: find a substring in a string
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
@RequestMapping
Problems encountered in fuzzy query of SQL statements
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
【工具跑SQL盲注】
Summary of training competition (Lao Li's collection of questions)
[SQL injection] joint query (the simplest injection method)
STM32 reverse entry
data2vec! New milestone of unified mode
M1 Pro install redis
Introduction to message queuing (MQ)
MPM model and ab pressure test
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
Silent authorization login and registration of wechat applet
"Niuke brush Verilog" part II Verilog advanced challenge