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 ...
随机推荐
- 重构23-Introduce Parameter Object(参数对象)
有 时当 使用 一个 包含 多 个 参 数 的 方 法 时 , 由 于参 数 过 多 会 导 致 可 读 性 严 重 下 降 , 如 : 有 时当 使用 一个 包含 多 个 参 数 的 方 法 时 , ...
- 安装 ant
(1)下载ant:http://ant.apache.org/bindownload.cgi (2)解压后放在指定的文件夹中(/usr/lib) (3) sudo chmod 777 /usr/lib ...
- Linux下VI的使用
vi使用手册 VI是unix上最常用的文本编辑工具,作为unix软件测试人员,有必要熟练掌握它.进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filena ...
- 在IIS上发布项目后浏览时报的错:Unable to make the session state request to the session state server
错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...
- ASP.Net上传中文文件乱码
只要在Head中添加即可解决:<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
- PAT1005—— 继续(3n+1)猜想
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...
- iOS UILabel自定义行间距时获取高度
本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...
- Part 30 to 31 Talking about Interfaces in c#
Part 30 Interfaces in c# We create interfaces using interface keyword. just like classes interfaces ...
- CentOS系统下安装以及卸载mysql
CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 [root@localhost `]$ wget htt ...
- 【学习笔记】【C语言】指针
一.指针变量的定义 1. 格式:变量类型 *指针变量名; 2. 举例:int *p; char *p2; 3. 注意:定义变量时的*仅仅是指针变量的象征 二.利用指针变量简单修改其他变量的值 1. ...