题意:

输入一个正整数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. root xshell登陆Ubuntu

    https://www.jianshu.com/p/c8ee39488d2a xshell测试非root用户,可以正常连接,但是root用户仍旧无法访问 解决方法:修改 /etc/ssh/sshd_c ...

  2. HDU1069 Monkey and Banana(dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:给定n种类型的长方体,每个类型长方体无数个,要求长方体叠放在一起,且上面的长方体接触面积要小于 ...

  3. elasticSearch 起步(CentOS7 success)

    安装和启动 这里使用docker进行安装的 docker pull elasticsearch 然后使用 docker run -e ES_JAVA_OPTS="-Xms256m -Xmx2 ...

  4. Web设计精髓(转)

    作者:zhouwenqi  地址:http://www.zhouwenqi.com/blog/board_43.html 这篇文章说实际问题的,所以不多强调,下面是我总结的一些比较突出的细节问题,而且 ...

  5. L2-3 名人堂与代金券

    题解 这题的话,每一个人都要占一个排名,即使排名并列了. 对于最后一个排名来说,即使人数超过了指定的k,也要加入. 代码 #include <bits/stdc++.h> using na ...

  6. Mysql中的触发器【转】

    转载:https://www.cnblogs.com/chenpi/p/5130993.html 阅读目录 什么是触发器 特点及作用 例子:创建触发器,记录表的增.删.改操作记录 弊端 什么是触发器 ...

  7. Hadoop TextInputFormat

    1. TextInputFortmat TextInputFormat是默认的InputFormat.每条记录是一行输入.Key是LongWritable类型,存储该行在整个文件中的字节偏移量(不是行 ...

  8. [C#] 委托与匿名方法

    using System; namespace 匿名函数 { class Program { delegate void TestDelegate(string s); static void M(s ...

  9. mockito使用教程

    一.什么是 Mock 测试 Mock 测试就是在测试过程中,对于某些不容易构造(如 HttpServletRequest 必须在Servlet 容器中才能构造出来)或者不容易获取比较复杂的对象(如 J ...

  10. Go_CSP并发模型

    go语言的最大两个亮点,一个是goroutine,一个就是chan了.二者合体的典型应用CSP,基本就是大家认可的并行开发神器,简化了并行程序的开发难度,我们来看一下CSP. 11.1.CSP是什么 ...