【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 ...
随机推荐
- [Codechef - AASHRAM] Gaithonde Leaves Aashram - 线段树,DFS序
[Codechef - AASHRAM] Gaithonde Leaves Aashram Description 给出一棵树,树的"N"节点根植于节点1,每个节点'u'与权重a[ ...
- linux连接oracle数据
//切换到oracle用户模式下 su - oracle //登录sqlplus sqlplus /nolog //连接orcale conn xx/xx;(用户名/密码) 或者 connect / ...
- linux命令 EOF
在shell脚本中,通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主Shell. EOF只是一个分界符,当然也可以用abcde替 ...
- python pymysql 基本使用
from pymysql import * # 1.创建连接数据库 conn = connect(host="localhost", port=3306, user="r ...
- codeforces 1288C. Two Arrays(dp)
链接:https://codeforces.com/contest/1288/problem/C C. Two Arrays 题意:给定一个数n和一个数m,让构建两个数组a和b满足条件,1.数组中所有 ...
- c# 泛型<T>类型参数T的约束where
在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制.如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误.这些限制称为约束.约束是使用 where 上 ...
- jenkins集成robot
一.jenkins集成robot的非gui的运行命令 pybot 配置文件 用例地址 或者robot 配置文件 用例地址 二.展示robot 运行结果图表 1.在系统配置中增加Rob ...
- scw——03错误,swagger开启错误
错误: 代码: @Value("${swagger2.enable:false}") private boolean enable = false; 原因:没有开启swagger的 ...
- TCP/IP详解,卷1:协议--第6章 ICMP:Internet控制报文协议
引言 I C M P经常被认为是 I P层的一个组成部分.它传递差错报文以及其他需要注意的信息. I C M P报文通常被I P层或更高层协议( T C P或U D P)使用.一些I C M P报文把 ...
- node安装升级过程中遇到的问题汇总
一.Node already installed, it's just not linked 第一步:sudo chown -R $(whoami) $(brew --prefix)/* 第二步:br ...