题意:

输入一个正整数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 分)的更多相关文章

  1. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  2. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  3. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  4. PAT 甲级 1099 Build A Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...

  5. 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 ...

  6. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

  7. 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 ...

  8. PAT甲级——A1099 Build A Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  9. 1099. Build A Binary Search Tree (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. Mapper-元素和属性

    Mapper.xml文件内部的元素和属性     parameterType(输入类型) §  传递简单类型 §  使用#{}占位符,或者${}进行sql拼接, #{}括号中的值可以任意, ${}括号 ...

  2. 番外:如何克隆可刷新的PDB(Refreshable PDB)

    基于版本:19c (12.2.0.3) AskScuti 创建方法:克隆创建 对应路径:属于克隆.PDB类型为:Refreshable 相关系列请参考<Oracle创建PDB列表文章> 注 ...

  3. java基础(四)之this的使用

    作用: 1.使用this调用成员变量和成员函数2.使用this调用构造函数 Person.java: class Person{ String name; //成员变量 void talk(Strin ...

  4. python3.0练习100题——001

    自学python3中,现在开始每天在python2.71 100例中做一道题,用python3实现,并写下一些思考-加油(ง •̀灬•́)ง 题目网站(http://www.runoob.com/py ...

  5. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  6. webpack4.41.0配置三(插件minCssExtract/ DefinePlugin/Html)

    (注:如无特殊说明这里的配置文件都指的是webpack.config.js) minCssExtract 我们通常期望js和js文件一起,css和css文件一起.该插件将CSS提取到单独的文件中.它为 ...

  7. Digital filter

    https://ww2.mathworks.cn/help/signal/examples/practical-introduction-to-digital-filter-design.html D ...

  8. django入门(一)

    小白一枚,老是感觉自己学了点什么东西马上就忘了,所以打算写点下来,以后可以看看,也希望能给以后点进来的人有一些帮助 本文是django的入门,现在在学,有错误之处还希望能包涵和指出,谢谢! 首先先下载 ...

  9. springboot引入Oracle依赖

    最近学习spring boot,在网上找一些项目学习,有的项目引入了oracle驱动包,自己搭建一直不成功,百度发现说是权限问题无法下载. 然后参考下面博客终于解决:springboot引入Oracl ...

  10. [MongoDB]MongoDB分页显示

    MongoDB Limit与Skip方法配合进行分页MongoDB Limit() 方法如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接 ...