当前位置:网站首页>1099 build a binary search tree (30 points)
1099 build a binary search tree (30 points)
2022-06-29 10:13:00 【Momo 623】
Answer key
Good question !!!
If I had done it before the ladder race last year It's really a good question
This question is the same as last year's ladder L2 The search tree There are similar places
I didn't make much achievements last year I thought that question could be withdrawn
It didn't turn out to be , According to a traversal sequence given by him I'm going through it again , Fill it in with this
This problem is given the template of binary tree , That question is almost given to
because That question is about a complete binary tree
So it's like giving
really · Answer key : In the properties of binary search tree , The middle order traversal sequence is sequential
So the data that will be given , Sort , Is the middle order sequence .
so , Know the appearance of middle order sequence and binary tree , Then we can get the whole binary tree
The resulting process is a basic tree traversal
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int n = 110;
int N;
int a[n], l[n], r[n], d[n];
int ind;
void build(int k)
{
if (k == -1)
return;
build(l[k]);
d[k] = a[ind++];
build(r[k]);
}
void bfs()
{
queue<int> q;
q.push(0);
while (!q.empty())
{
int x = q.front();
q.pop();
if (x != 0)
cout << " ";
cout << d[x];
// cout << x << endl;
if (l[x] != -1)
q.push(l[x]);
if (r[x] != -1)
q.push(r[x]);
}
}
int main()
{
cin >> N;
int x, y;
for (int i = 0; i < N; i++)
{
cin >> x >> y;
l[i] = x;
r[i] = y;
}
for (int i = 0; i < N; i++)
cin >> a[i];
sort(a, a + N);
build(0);
bfs();
return 0;
}
边栏推荐
猜你喜欢

Codeforces Round #645 (Div. 2)

Dynamic linking of virtual machine stack of JVM

A 2.5D Cancer Segmentation for MRI Images Based on U-Net

The collapsing "2.3 * 10 = 22" produced by multiplying float and int

Time varying and non time varying

点在多边形内外的判断

Codeforces Round #659 (Div. 2)

十六制计数器和流水灯

JVM method return address

HDU 6778 car (group enumeration -- > shape pressure DP)
随机推荐
自定义控件之侧滑关闭 Activity 控件
Listview of the basic component of the shutter
Leetcode MySQL database topic 176
L2-031 深入虎穴 (25 分)
L1-009 sum of N numbers (20 points)
Application of keil5 integrated development environment for single chip microcomputer
任务调度器之Azkaban的使用
Codeforces Round #659 (Div. 2)
A method of creating easy to manage and maintain thread by C language
51nod1277 maximum value in string [KMP]
EDA and VHDL question bank
GridView of basic component of shutter
EDA与VHDL题库
sympy的dsolve函数
2019.10.16 training summary
Memory layout of JVM objects
Leetcode MySQL database topic 181
1099 Build A Binary Search Tree (30 分)
JNI. H description
Nacos environmental isolation