当前位置:网站首页>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;
}边栏推荐
- [set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
- Problems encountered in fuzzy query of SQL statements
- Leetcode simple problem delete an element to strictly increment the array
- Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
- Number of uniform strings of leetcode simple problem
- Keepalived热备与HAProxy
- Hire cashier (differential constraint)
- The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
- UiPath实战(08) - 选取器(Selector)
- On typescript and grammar
猜你喜欢

Flutter monitors volume to realize waveform visualization of audio

On typescript and grammar

String matching: find a substring in a string

Thesis reading_ ICD code_ MSMN

Retirement plan fails, 64 year old programmer starts work again

Games101 Lesson 9 shading 3 Notes
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes

带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...

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

Apache MPM model and ab stress test
随机推荐
Valentine's day limited withdrawal guide: for one in 200 million of you
联发科技2023届提前批IC笔试(题目)
《牛客刷verilog》Part II Verilog进阶挑战
Leetcode simple problem delete an element to strictly increment the array
Shell script -- condition judgment
Leetcode simple question: check whether the string is an array prefix
Distinguish between releases and snapshots in nexus private library
stm32逆向入门
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
论文阅读_清华ERNIE
Concurrent operation memory interaction
Thesis reading_ Chinese NLP_ ELECTRA
Number of uniform strings of leetcode simple problem
SSM framework integration
Preparation for school and professional cognition
I stepped on a foundation pit today
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
Learn to use the idea breakpoint debugging tool
Notes | numpy-10 Iterative array