当前位置:网站首页>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;
}边栏推荐
- Keepalived热备与HAProxy
- Leetcode simple question: check whether the string is an array prefix
- Day 51 - tree problem
- [research materials] annual report of China's pension market in 2021 - Download attached
- Interface frequency limit access
- Thesis reading_ ICD code_ MSMN
- MPM model and ab pressure test
- C primre plus Chapter 10 question 6 inverted array
- The least operation of leetcode simple problem makes the array increment
- 移动端——uniapp开发记录(公共请求request封装)
猜你喜欢

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution

Mobile terminal - uniapp development record (public request encapsulation)

论文阅读_中文医疗模型_ eHealth

Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis

Learn to use the idea breakpoint debugging tool

Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
![[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached](/img/95/833f5ec20207ee5d7e6cdfa7208c5e.jpg)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
![[tools run SQL blind note]](/img/c3/86db4568b221d2423914990a88eec2.png)
[tools run SQL blind note]
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes

The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
随机推荐
Silent authorization login and registration of wechat applet
Notes | numpy-09 Broadcast
Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
逆袭大学生的职业规划
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Shell script Basics - basic grammar knowledge
Keepalived热备与HAProxy
Retirement plan fails, 64 year old programmer starts work again
移动端——uniapp开发记录(公共请求request封装)
String matching: find a substring in a string
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
[research materials] annual report of China's pension market in 2021 - Download attached
The least operation of leetcode simple problem makes the array increment
JS multidimensional array to one-dimensional array
Learn to use the idea breakpoint debugging tool
【XSS绕过-防护策略】理解防护策略,更好的绕过
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
Learning practice: comprehensive application of cycle and branch structure (I)
Leetcode simple question: check whether the array is sorted and rotated
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection