PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)
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
分析
这道题就是已知一个二叉搜索树的树形和节点的元素,叫你把元素填进去,然后输出层序遍历的结果,其实由二叉搜索树中序遍历的性质可知其中序遍历的结果是升序的,所以先把节点元素从小到大排序,这就是二叉搜索树的中序遍历的结果,再把二叉搜索树的节点下标按中序遍历输出与前面的元素一一对应就可以获得了二叉搜索树填进去的结果了,最后层序输出就OK了。
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct child{
int left,right;
};
int n,t=0;
vector<child> children(100);
vector<int> a(100),b(100);
void inorder(int index){
if(children[index].left!=-1)
inorder(children[index].left);
b[index]=a[t++];
if(children[index].right!=-1)
inorder(children[index].right);
}
int main(){
int tag=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>children[i].left>>children[i].right;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.begin()+n);
inorder(0);
queue<int> q;
q.push(0);
while(!q.empty()){
int temp=q.front();
q.pop();
tag++>0?cout<<" "<<b[temp]:cout<<b[temp];
if(children[temp].left!=-1)
q.push(children[temp].left);
if(children[temp].right!=-1)
q.push(children[temp].right);
}
return 0;
}
PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)的更多相关文章
- 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 (二叉搜索树)
本文同步发布在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 ...
- 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 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) 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 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历
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 ...
随机推荐
- influx测试——单条读性能很差,大约400条/s,批量写性能很高,7万条/s,总体说来适合IOT数据批量存,根据tag查和过滤场景,按照时间顺序返回
测试准备 需要将InfluxDB的源码放入 go/src/github.com/influxdata 目录 单写测试代码(write1.go): package main import ( " ...
- Ubuntu中安装部署Intel CS WebRTC
1环境要求 组件 版本要求 OS CentOS* 7.4, Ubuntu 14.04/16.04 LTS Node 8.11.* (推荐8.11.1) MongoDB 2.4.9 Boost 1.65 ...
- Akka源码分析-Remote-网络链接
上一篇博客中,我们分析了Akka remote模式下消息发送的过程,但细心的读者一定发现没有介绍网络相关初始化.创建链接.释放链接的过程,本文就介绍一下相关的内容. 网络初始化就离不开ActorSys ...
- Akka源码分析-ActorSystem
由于本人对Akka比较感兴趣,也用Akka开发了一些系统,但对Akka的源码还没有具体分析过,希望研究源码的同时写一点博客跟大家分享.有不当之处还请指正.我准备采取Debug的方式来研究Akka的运行 ...
- BZOJ 4530 LCT/线段树合并
//By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using nam ...
- android 二维码 扫描,生成,竖屏
最近公司有用到二维码,生成,扫描,所以学习了一下,和大家分享: demo 见下面链接,已经改成竖屏: http://download.csdn.net/detail/q610098308/868101 ...
- Laravel5.1学习笔记15 数据库1 数据库使用入门
简介 运行原生SQL查询 监听查询事件 数据库事务 使用多数据库连接 简介 Laravel makes connecting with databases and running queries e ...
- Laravel5.1学习笔记13 系统架构5 Contract
Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...
- python gdal 矢量转栅格
data = gdal.Open(templateTifFileName, gdalconst.GA_ReadOnly)geo_transform = data.GetGeoTransform()x_ ...
- Floyd模板
比较简单的算法:但是当点太多需要剪枝,不然很耗时 void Floyd() { ;k<n;++k) ;i<n;++i) ;j<n;++j) dj[i][j] = min(dj[i][ ...