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 ...
随机推荐
- 基于第三方开源库的OPC服务器开发指南(3)——OPC客户端
本篇将讲解如何编写一个OPC客户端程序测试我们在前文<基于第三方开源库的OPC服务器开发指南(2)——LightOPC的编译及部署>一篇建立的服务器.本指南的目的是熟悉OPC服务器的开发流 ...
- [笔记]Laravel TDD 胡乱记录
TDD: 测试驱动开发(Test-Driven Development),TDD的原理是在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码. -- 载自TDD百度百科 参考 ...
- C盘清理记——罪魁Visual Studio
话不啰嗦,单刀直入:在C:\ProgramData\Microsoft Visual Studio文件夹下,VS会自动记录IntelliTrace File,久而久之,会无限消耗磁盘空间,直接到塞满C ...
- php设置时区和strtotime转化为时间戳函数
date_default_timezone_set('PRC');//设置中华人民共和国标准时间 strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 格式:int strto ...
- luoguP1062 数列 [数学]
题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… (该序列实际上就是 ...
- JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案
现在很多系统都有导出excel的功能,总结一下自己之前写的,希望能帮到其他人,这里我用的是XSSFWorkbook,我们项目在winsang 用的Tomcat,LInux上用的weblogic服务器, ...
- Python接口测试框架实战与自动化进阶✍✍✍
Python接口测试框架实战与自动化进阶 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看 ...
- Nvelocity 语法
原文:Nvelocity 语法 1,数字循环 #foreach($i in [0..9]) $i #end 2,dictionary 根据key获取value值 #set($key1=&q ...
- vue qs插件的使用
参考:https://blog.csdn.net/weixin_43851769/article/details/86505164 qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库. 步 ...
- UBOOT命令简写说明
所以命令都可以简写,只要命令前面的一部分不会跟其它命令相同,就可以不用写全整个命令. save 命令 CRANE2410 # sa Saving Environment to Flash... Un- ...