【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 ...
随机推荐
- SSM-整合简单配置
首先说明Spring和Mybatis的版本: Spring:3.2.4 Mybatis:3.3.0 使用了C3P0连接池和Log4J日志,下面是jar包总览: 然后是项目目录总览: 为了能够让项目跑一 ...
- DM9000C网卡驱动程序编写与测试
一般网卡驱动程序厂商会给我们提供一份模板驱动,我们的工作就是需要根据自己的需要更改这个模板驱动 1.DM9000C的硬件连接 硬件连接图如下所示:它接在S3C2440的BANK4内存控制器上,它只占用 ...
- 怎么把html页面部署到云服务器上
1,下载nginx 2,把页面放置到云服务器上 3,通过配置nginx conf下的nginx.conf文件,就可以通过ip:port访问到了 链接:https://www.cnblogs.com/f ...
- 前端框架vue.js系列(9):Vue.extend、Vue.component与new Vue
前端框架vue.js系列(9):Vue.extend.Vue.component与new Vue 本文链接:https://blog.csdn.net/zeping891103/article/det ...
- Light Up Your Business Promotions With LED Keychain
Imagine you want to insert the car key into the keyhole in the dark. What would you do? You will def ...
- 每天进步一点点------Allegro PCB
Allegro PCB 1.如何在allegro中取消花焊盘(十字焊盘) set up->design parameter ->shape->edit global dynamic ...
- 【转载】Java集合容器全面分析
转自:http://blog.csdn.net/garfielder007/article/details/52143803 简介: 集合类Collection不是Java的核心类,是Java的扩展类 ...
- CSS input
去除激活 input 的默认边框 // 三种方法都能实现 input{ outline: none; outline: medium; outline:; } 修改光标颜色 input{ outl ...
- Collection两个常见的集合类型: ArrayList可重复集有序 ,HashSet不可重复集
package seday11; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; ...
- 线性递推BM模板
#include <cstdio> #include<iostream> #include <cstring> #include <cmath> #in ...