当前位置:网站首页>1099 Build A Binary Search Tree (30 分)
1099 Build A Binary Search Tree (30 分)
2022-06-29 09:23:00 【陌陌623】
题解
好题!!!
如果我去年天梯赛之前就做过的话 真的是好题了
这个题和去年天梯L2的搜索树 有相似的地方
我去年还不怎么会建树 以为那个题可以退出来的
结果不是,是按着他给的一个遍历序列 在过一遍,以此填进去
这个题是给了二叉树的模板,那个题也差不多是给了
因为 那个题说的是完全二叉树
所以和给差不多
真·题解: 在二叉搜索树的性质中,中序遍历序列是顺序的
所以将给出的数据,排序,就是中序序列。
故,知道了中序序列和二叉树的样子,那么就可以得到整个二叉树
得到的过程就是一个基本的树的遍历
#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;
}
边栏推荐
- RecyclerView刷新闪烁与删除Item时崩溃问题
- Codeforces Round #659 (Div. 2)
- Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
- If I were in Beijing, where would it be better to open an account? In addition, is it safe to open an account online now?
- Gmail: how to quickly read all messages
- 详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
- setInterval、setTimeout和requestAnimationFrame
- Flutter 基础组件之 Container
- 逆向思维-小故事
- RecyclerView 粘性(悬浮)头部
猜你喜欢

Constructing SQL statements by sprintf() function in C language

Cisco ASA、FTD和HyperFlex HX的漏洞分析复现

Generic paging framework

Dynamic linking of virtual machine stack of JVM

容器

Listview of the basic component of the shutter

Student addition / deletion gaih

力扣94二叉树的中序遍历

Alternative implementation of Scrollview pull-down header amplification

JVM之方法返回地址
随机推荐
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
setInterval、setTimeout和requestAnimationFrame
2019icpc上海区域赛赛后总结
2020-09-25 boost库的noncopyable,用于单例模式
gSoap例子——calc
Signal works: time varying and time invariant
Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
Container of the basic component of the flutter
2019.11.3学习总结
Flutter 基础组件之 ListView
Power Strings【KMP循环节】
C语言实现一种创建易管理易维护线程的方法
语言特性
单片机集成开发环境Keil5的使用
If I were in Beijing, where would it be better to open an account? In addition, is it safe to open an account online now?
同花顺炒股软件可靠吗,安全吗?
Nacos环境隔离
GridView of basic component of shutter
nacos注册中心集群
JVM之方法的绑定机制