PAt 1099
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format left_index right_index
, provided that the nodes are numbered from 0 to N−1, and 0 is always the root. If one child is missing, then − will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:
9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42
Sample Output:
58 25 82 11 38 67 45 73 42
//根据二叉树的结构和中序遍历来写出层序遍历
#include <bits/stdc++.h>
using namespace std;
#define N 120
#define P pair<int,int>
struct Tree{
int val,l,r;
Tree(){
}
Tree(int val,int l,int r):val(val),l(l),r(r){
}
}tr[N];
int a[N],b[N],n;
//不需要建树,题目输入的就是了
int cnt2=;
void search(int rt){//中序
if(rt==-) return ;
search(tr[rt].l);
tr[rt].val=a[cnt2++];
search(tr[rt].r);
}
int cnt = ;
void dfs(int rt){//层序遍历
queue<P>q;
q.push(P(rt,tr[rt].val));
int flag =;
while(!q.empty()){
P p =q.front();q.pop();
int x=p.first,y=p.second;
//输出格式
if(flag){
printf("%d",y);
flag =;
}
else{
printf(" %d",y);
}
int num1 = tr[x].l;
int num2= tr[x].r;
if(num1!=-){
q.push(P(num1,tr[num1].val));
}
if(num2!=-){
q.push(P(num2,tr[num2].val));
}
}
}
int main()
{
scanf("%d",&n);
for(int i =;i<n;i++){
scanf("%d%d",&tr[i].l,&tr[i].r);
}
for(int i =;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
search();
dfs();
return ;
}
PAt 1099的更多相关文章
- PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- PAT 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
随机推荐
- 51nod1803 森林直径
[传送门] 考虑计算直径的 dp 方法. $d[u]$ 表示以 $u$ 为根的子树能 $u$ 能走到的最远距离 $dp[u]$ 表示以 $u$ 为根的子树的直径那么每次dfs一个子节点后$dp[u] ...
- RPC笔记搬迁
选择dubbo 启动原理 解析服务 暴露服务 引用服务 提供服务流程 结合Netty 对比 HSF https://www.cnblogs.com/lichengwei/p/5529492.h ...
- 安装单机es
1.安装JDK(1.8)2.上传解压Elasticsearch-5.4.33.创建一个普通用户,然后将对于的目录修改为普通用户的所属用户和所属组4.修改配置文件config/elasticsearch ...
- smashing 三方widgets 使用
smashing 有一套自己的约定,包括widgets 以及dashboard,还有就是关于数据的处理 约定如下 三方widgets 统一在widgets 目录下,一般包含了基于coffee 的js ...
- 洛谷 P2815 IPv6地址压缩 题解
P2815 IPv6地址压缩 题目背景 (友情提示:IPv6基础知识曾多次出现在NOIP初赛中)Internet Protocol,互联网协议,即为我们常说的IP.我们目前常说的IP主要指它的第四版, ...
- HTML引入外部JS文件
<!--引入外部文件的方式--> <script type="text/javascript" src="attack.js">< ...
- Content-type解析
一.是什么? 是Http的实体首部字段,用于说明请求或返回的消息主体是用何种方式编码,在request header和response header里都存在. Content-Type(内容类型),一 ...
- 简单与实用:SpringMVC的常见使用
一.前言 现在的项目大多数都是使用SpringMVC作为MVC框架.SpringMVC的学习成本较低,容易上手,简单实用. 二.应用 1.@Controller & @RequestMappi ...
- js - try..catch详解
try... catch 1.try...catch和if语句 为什么不用if替换调try...catch? 大部分人都有这样想法 if=>只能判断用户操作 try...catch=>来自 ...
- linux下检查网络连通情况
MTR是一种简单的跨平台命令行网络诊断工具,它将常用的traceroute和ping程序的功能组合到一个工具中. 与traceroute类似, mtr输出关于数据包从运行mtr的主机到用户指定的目标主 ...