PAT甲级——A1099 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 (≤) 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 <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct Node
{
int val, l, r;
}node[];
vector<int>nums(), levelOrder;
int N, k = ;
void inOrderTravel(int root)//得到树的中序遍历
{
if (root == -)
return;
inOrderTravel(node[root].l);
node[root].val = nums[k++];
inOrderTravel(node[root].r);
}
void levelOrderTravel(int root)//得到树的中序遍历
{
queue<int>q;
q.push(root);
while (!q.empty())
{
root = q.front();
q.pop();
levelOrder.push_back(node[root].val);
if (node[root].l != -)
q.push(node[root].l);
if (node[root].r != -)
q.push(node[root].r);
}
}
int main()
{
cin >> N;
int l, r;
int root = ;
for (int i = ; i < N; ++i)//按题目意思使用前序遍历构建一棵树
{
cin >> l >> r;
node[i].l = l;
node[i].r = r;
}
for (int i = ; i < N; ++i)
cin >> nums[i];
sort(nums.begin(), nums.begin() + N);//得到中序遍历
inOrderTravel(root);//通过中序遍历重构二叉树
levelOrderTravel(root);
for (int i = ; i < N; ++i)
cout << levelOrder[i] << (i == N - ? "" : " ");
return ;
}
PAT甲级——A1099 Build A Binary Search Tree的更多相关文章
- 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 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- A1099. Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT_A1099#Build A Binary Search Tree
Source: PAT A1099 Build A Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recur ...
随机推荐
- echarts数据变了不重新渲染,以及重新渲染了前后数据会重叠渲染的问题
1.echarts数据变了但是视图不重新渲染 新建Chart.vue文件 <template> <p :id="id" :style="style&q ...
- LINUX centos 7.2/7.3 搭建LAMP环境
首先我们先查看下centos的版本信息 #适用于所有的linux lsb_release -a #或者 cat /etc/redhat-release #又或者 rpm -q centos-relea ...
- 关于soapui如何做安全测试
1.首先安装soapui5.1.2 第一步:运行SoapUI-Pro-x32-5.1.2_576024.exe文件,按照步骤安装成功: 第二步:拷贝Protection-4.6.jar到soapui安 ...
- Android开发 AAC的ADTS头解析[转载]
原文地址:https://www.jianshu.com/p/b5ca697535bd 1. ADTS(Audio Data Transport Stream)头之于AAC AAC音频文件的每一帧都由 ...
- css---8 过渡属性刨析
1. transition-property 默认值为 all,表示所有可被动画的属性都表现出过渡动 可以指定多个 property 属性值: none 没有过渡动画. all 所有可被动 ...
- mysql主从同步的键值冲突问题的解决方法
转自https://njs375666635.iteye.com/blog/2242067 多主互备和主从复制有一些区别,因为多主中都可以对服务器有写权限,所以设计到自增长重复问题 出现的问题(多主自 ...
- 重磅发布: 阿里云WAF日志实时分析上线 (含视频)
摘要: 阿里云WAF与日志服务打通,对外开发Web访问与攻击日志.提供近实时的网站具体的日志自动采集存储.并提供基于日志服务的查询分析.报表报警.下游计算对接与投递的能力. 背景 Web攻击形势 互联 ...
- MYSQL - database 以及 table 的增删改查
MYSQL - database 以及 table 的增删改查 MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这 ...
- 0908CSP-S模拟测试赛后总结
我早就料到昨天会考两场2333 话说老师终于给模拟赛改名了啊. 距离NOIP祭日还有60天hhh. 以上是废话. %%%DeepinC无敌神 -rank1 zkt神.kx神.动动神 -rank2 有钱 ...
- javascript中onclick(this)用法和onclick(this.value)用法介绍
onclick(this.value)代码详解 <html> <head> <script language="javascript"> fun ...