当前位置:网站首页>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;
}
边栏推荐
- 力扣85题最大矩形
- 弧形 View 和弧形 ViewPager
- HDU 6778 Car (分组枚举-->状压 dp)
- JS obtain mobile phone model and system version
- 2019.10.30学习总结
- A 2.5D Cancer Segmentation for MRI Images Based on U-Net
- 我想知道如何免费网上注册股票开户?另外,手机开户安全么?
- Summary of PHP memory horse technology research and killing methods
- 51nod1277 字符串中的最大值【KMP】
- 2020-09-17 gateway业务流程 两个任务:referer认证和非商品模板化
猜你喜欢
随机推荐
Image of the basic component of the shutter
十六制计数器和流水灯
Dsolve function of sympy
RecyclerView刷新闪烁与删除Item时崩溃问题
ORA-01950 对表空间无权限
详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
51nod1277 字符串中的最大值【KMP】
FreeRTOS(八)——时间管理
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
信号作品:时变和时不变
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
基辅周边的凄美废墟——切尔诺贝利的安全前往指南!
Generic paging framework
2019.11.13训练总结
Dynamic linking of virtual machine stack of JVM
2019.10.16训练总结
FreeRTOS (IX) - queue
力扣94二叉树的中序遍历
HDU 6778 Car (分组枚举-->状压 dp)








