1099. Build A Binary Search Tree (30)
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 (<=100) 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 -1 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<stdio.h>
#include<math.h>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std; struct node
{
int l,r,v;
}; node Tree[];
vector<int> vv;
int cnt = ;
void inOder(int root)
{
if(Tree[root].l != -)
inOder(Tree[root].l);
Tree[root].v = vv[cnt++];
if(Tree[root].r != -)
inOder(Tree[root].r);
} int main()
{
int n,tem;
scanf("%d",&n);
for(int i = ;i < n;++i)
{
scanf("%d%d",&Tree[i].l,&Tree[i].r);
} for(int i = ;i < n;++i)
{
scanf("%d",&tem);
vv.push_back(tem);
}
sort(vv.begin(),vv.end());
inOder();
queue<node> qq;
qq.push(Tree[]);
bool fir = ;
while(!qq.empty())
{
node ntem = qq.front();
qq.pop();
if(fir)
{
fir = ;
printf("%d",ntem.v);
}
else
{
printf(" %d",ntem.v);
}
if(ntem.l != -)
qq.push(Tree[ntem.l]);
if(ntem.r != -)
qq.push(Tree[ntem.r]);
}
printf("\n");
return ;
}
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 (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 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 (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 (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
随机推荐
- Android中GridLayout与GridView区别
GridLayout初步研究:可以这么说这个布局绝对是开发者的福音,它大大简化了对复杂布局的处理,包括性能提高不是一倍两倍.它与GridView是完全不同的概念, GridView是一种适配器布局,它 ...
- WebStrom9 体验nodejs
之前就有体验过 WebStrom8.0.3 版本,确实不错. 最喜欢的是集成了Terminal 很方便的使用NPM,今天装上发现 Terminal 死活打不上字.什么原因! WebStrom9 在wi ...
- Shell学习笔记 - Shell变量
一.变量的命名 变量名必须以字母或下划线开头,由字母.数字.或下划线组成,变量名的长度不能超过255个字符. 二.变量的分类 1. 用户自定义变量 2. 环境变量 3. 位置参数变量 4. 预定义变量 ...
- uva 12549 最大流
思路:这题的原型题是比较经典的网络流.原型题模型就是把所有的障碍去掉. 有障碍做法还是一样的,只用将每个列和行重新划分,求最大流就行了. #include <cstring> #inclu ...
- poj 2029 二维树状数组
思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...
- hdu 4669 模拟
思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...
- 全新jquery多点滑动幻灯片——全屏动画animateSlide
首页banner的酷炫效果多来自全屏大图的幻灯片动画,下面提供一种完美兼容的jquery动画特效:全新jquery多点滑动幻灯片——全屏动画animateSlide(代码完全原创). 直接上代码,把h ...
- java之旅——JDK版本下载
作为一名IT工作者,技术学无止境,最近开始学习java. 学习java就需要安装jdk,直接到官网上下载,总是找不到很好的版本,在资源中找到一个下载jdk的链接,想下载哪个版本都有. http://w ...
- 【转】自己动手写SC语言编译器
自序 编译原理与技术的一整套理论在整个计算机科学领域占有相当重要的地位,学习它对程序设计人员有很大的帮助.我们考究历史会发现那些人人称颂的程序设 计大师都是编译领域的高手,像写出BASIC语言的BIL ...
- Part 98 Anonymous methods in c#
What is an anonymous method? Anonymous method is a method without a name. Introduced in C# 2.0,they ...