当前位置:网站首页>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;
}边栏推荐
- Mobile terminal - uniapp development record (public request encapsulation)
- Introduction to JVM principle
- Summary of training competition (Lao Li's collection of questions)
- Introduction to message queuing (MQ)
- Market status and development prospect forecast of global heat curing adhesive industry in 2022
- Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
- Thesis reading_ Tsinghua Ernie
- [set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
- Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
- Learning record of arouter principle
猜你喜欢

The least operation of leetcode simple problem makes the array increment

The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping

Flutter monitors volume to realize waveform visualization of audio

并发操作-内存交互操作

Interface frequency limit access

UiPath实战(08) - 选取器(Selector)

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

Leetcode simple question: check whether the array is sorted and rotated

论文阅读_ICD编码_MSMN

MC Layer Target
随机推荐
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
MySQL winter vacation self-study 2022 12 (3)
Introduction to message queuing (MQ)
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
C primre plus Chapter 10 question 6 inverted array
Shuttle + alluxio accelerated memory shuffle take-off
[develop wechat applet local storage with uni app]
[USACO 2009 Dec S]Music Notes
Hj35 serpentine matrix
关于开学的准备与专业认知
【SQL注入点】注入点出现位置、判断
Number of uniform strings of leetcode simple problem
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
data2vec! New milestone of unified mode
Web security - CSRF (token)
Market status and development prospects of the global autonomous marine glider industry in 2022
Notes | numpy-07 Slice and index
论文阅读_中文医疗模型_ eHealth
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article