【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意:
输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值。输出这颗二叉排序树的层次遍历。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
pair<int,int>a[];
int b[];
int cnt=;
int ans[];
void dfs(int x){
if(x==-)
return ;
dfs(a[x].first);
ans[x]=b[++cnt];
dfs(a[x].second);
}
void bfs(int x){
queue<int>q;
q.push(x);
while(!q.empty()){
int now=q.front();
q.pop();
if(a[now].first!=-)
q.push(a[now].first);
if(a[now].second!=-)
q.push(a[now].second);
if(now!=x)
cout<<" ";
cout<<ans[now];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i){
int x,y;
cin>>x>>y;
a[i-]={x,y};
}
for(int i=;i<=n;++i)
cin>>b[i];
sort(b+,b++n);
dfs();
bfs();
return ;
}
【PAT甲级】1099 Build A Binary Search Tree (30 分)的更多相关文章
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- PAT甲级——A1099 Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- 第一篇 网站基础知识 第5章 自己动手实现HTTP协议
第5章 自己动手实现HTTP协议 我们知道HTTP协议是在应用层解析内容的,只需要按照它的报文的格式封装和解析数据就可以了,具体的传输还是使用的Socket,在第4章NioServer的基础上自己做一 ...
- HTML5使用JavaScript控制<audio>音频的播放
1.播放音乐最简单的样例 <audio controls> <source src="horse.mp3" type="audio/mpeg" ...
- (转)HDFS简介
转自:http://os.51cto.com/art/201212/369564.html
- mysql update某个字段替换成某个值
UPDATE xc_admin_vehicle SET brandid= REPLACE(brandid, 19, 54) UPDATE xc_admin_models SET bid= REPLAC ...
- 易错之 Java字符串比较
字符串比较 不能直接用==判断,因为字符串内存地址不同,等号比较的是地址而不是大小 用equals()判断字符串是否相等 还可以用compareTo()比较
- torchvision的理解和学习 加载常用数据集,对主流模型的调用.md
torchvision的理解和学习 加载常用数据集,对主流模型的调用 https://blog.csdn.net/tsq292978891/article/details/79403617 加载常用数 ...
- 格式化输出_python
一.直接使用 +a='chen'b='xiao'c='zan'print(a+b+c) 二.利用占位符 %%s:占位符:%d:整数:%x:十六进制:%f:浮点数(默认6位小数)特别注意浮点数: 指定长 ...
- webpack配置的说明
{devtool: 'source-map',//要启用source-map需加上此配置项,同时css或less的loader要加上参数?sourceMap,js的loader不用加 entry: e ...
- HTML学习(14)表单
HTML 表单用于收集不同类型的用户输入. HTML 表单 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中输入内容,比如:文本域(textarea).下拉列表.单选框(radio-butt ...
- codeforces C. Primes and Multiplication(快速幂 唯一分解定理)
题目链接:http://codeforces.com/contest/1228/problem/C 题解:给定一个函数f,g,题目有描述其中的表达式含义和两者之间的关系. 然后计算: 首先把给定的x用 ...