当前位置:网站首页>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;
}
边栏推荐
- Why does I start with =1? How does this code work?
- The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
- Sdl2 + OpenGL glsl practice (Continued)
- Do you know UVs in modeling?
- Leetcode simple problem delete an element to strictly increment the array
- Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
- Number of uniform strings of leetcode simple problem
- On typescript and grammar
- Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
- Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
猜你喜欢
The least operation of leetcode simple problem makes the array increment
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
Source insight garbled code solution
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
MPM model and ab pressure test
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
Preparation for school and professional cognition
Handler understands the record
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
Basic use of Metasploit penetration testing framework
随机推荐
论文阅读_ICD编码_MSMN
MPM model and ab pressure test
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
Wechat applet waterfall flow and pull up to the bottom
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Wechat applet distance and map
Market status and development prospect forecast of global button dropper industry in 2022
[research materials] 2021 China's game industry brand report - Download attached
Market status and development prospects of the global autonomous marine glider industry in 2022
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
Thesis reading_ Tsinghua Ernie
【工具跑SQL盲注】
Current market situation and development prospect forecast of the global fire boots industry in 2022
第十九届浙江省 I. Barbecue
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Leetcode simple question: the key with the longest key duration
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
General undergraduate college life pit avoidance Guide
[BMZCTF-pwn] 18-RCTF-2017-Recho
Problems encountered in fuzzy query of SQL statements