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: # ...
随机推荐
- Reactive Extensions (Rx) 入门(2) —— 安装 Reactive Extensions
译文:https://blog.csdn.net/fangxing80/article/details/7581937 原文:http://www.atmarkit.co.jp/fdotnet/int ...
- vs2017使用OpenGL的方法
第一步:将相应的.lib和.h文件放到相应的文件夹中,具体如下: 将GLAUX.LIB,GLU32.LIB,OPENGL32.LIB 放到....\VC\Tools\MSVC\14.10.25017\ ...
- 洛谷P3834题解
若想要深入学习主席树,传送门. Description: 给定数列 \(\{a_n\}\) ,求闭区间 \([l,r]\) 的第 \(k\) 小的数. Method: 先对数据进行离散化,然后按照权值 ...
- 关于C++中extern的简单笔记
extern可以实现多文件共享同一个变量.const常量.函数. 下面结合几个例子来讲一下extern的相关性质(下述皆为多文件编译): 例1: //file1.cpp #include<ios ...
- pandas把'<m8[ns]'类型转换为int类型进行运算
工作中经常碰到两列数据为date类型,当这两列数据相减或者相加时,得到天数,当运用这个值进行运算会报错:ufunc true_divide cannot use operands with types ...
- linux 去掉 ^M 的方法
在linux上经常遇到这种问题,从网上下载文件到 linux 上后,就多了很多 ^M这种东西,如何集体删除这种东西呢! 用 vim 打开文件 进行如下设置 将文件格式转化为unix :set ff= ...
- ubuntu之路——day19.1 深度CNN的探究
1.经典的CNN LeNet-5 1998的CNN鼻祖 以前用的sigmoid和tanh 下图给的是relu和softmax AlexNet ImageNet2012的冠军 VGG-16 ImageN ...
- 【面向对象】第四单元总结——UML
本单元构架设计 统一建模语言(英语:Unified Modeling Language,缩写 UML)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的 ...
- RNN 权重共享
之前在几篇博客中说到了权重共享,但都觉得不够全面,这里做个专题,以后有新的理解都在此更新. 1. 减少运算只是锦上添花之前说到权重共享可以减少运算,是的,但这样说好像是可有可无,只是运算量大小的问题, ...
- 站在BERT肩膀上的NLP新秀们(PART I)
站在BERT肩膀上的NLP新秀们(PART I)