PAT 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648
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 Nlines 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 <bits/stdc++.h>
using namespace std; int N; struct Node{
int data;
int l, r;
}node[110]; vector<int> v;
int ans = 0; void inorder(int root) {
if(root == -1) return ; inorder(node[root].l);
node[root].data = v[ans ++];
inorder(node[root].r);
} void levelorder(int root) {
queue<int> q;
if(root != -1) q.push(root);
bool isfirst = true; while(!q.empty()) {
int t = q.front();
q.pop(); if(isfirst) {
printf("%d", node[t].data);
isfirst = false;
}
else printf(" %d", node[t].data); if(node[t].l != -1) q.push(node[t].l);
if(node[t].r != -1) q.push(node[t].r);
}
} int main() {
scanf("%d", &N);
for(int i =0; i < N; i ++)
scanf("%d%d", &node[i].l, &node[i].r);
v.resize(N);
for(int i = 0; i < N; i ++)
scanf("%d", &v[i]);
sort(v.begin(), v.end());
inorder(0);
levelorder(0);
return 0;
}
这这这 应该是学会建树了叭 unbelieveable 有一点点鸡冻
PAT 甲级 1099 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甲级——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 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 ...
- 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甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 (树的中序,层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- django中models的filter过滤方法
__gt 大于__gte 大于等于 __lt 小于 __lte 小于等于 __in 存在于一个list范围内 __startswith 以...开头 __is ...
- 20155204 2016-2017-2 《Java程序设计》第10周学习总结
20155204 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 1.计算机网络概述 路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控 ...
- (三)虚拟机与Linux新尝试——20155306白皎
(三)虚拟机与Linux新尝试--20155306白皎 一.关于虚拟机的安装 在选择虚拟机的类型和版本时,Ubuntu只有32位,没有64位 解决:通过百度,后来也发现同学们好多遇到了这个问题,因此通 ...
- 20155310 2016-2017-2 《Java程序设计》第四周学习总结
20155310 2016-2017-2 <Java程序设计>第四周学习总结 一周两章新知识的自学与理解真的是很考验和锻炼我们,也对前面几章我们的学习进行了检测,遇到忘记和不懂的知识就再复 ...
- 20155333 2016-2017-2 《Java程序设计》第三周学习总结
20155333 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章 类定义时使用class关键词,名称使用Clothes,建立实例要使用new关键词. ...
- 3.MUI端APP获取Json数据,并且实现遍历
在MUI中,对JSON的操作还是非常方便的. <script type="text/javascript"> var responseEl = document.get ...
- js日期控件遇到的问题
一.问题: 在web项目里有很多时候需要使用日期控件来完成相关的功能,但是日期控件的日期格式又和我们的需求不符 那么,就需要我们来自定义日期的格式完成需求 二.解决: 1.取月末: (1)强制取值: ...
- Mybatis传递参数的三种方式
第一种: Dao层使用@Param注解的方法 VersionBox getVersionByVersionNumAndVersionType(@Param("versionNum" ...
- [备忘]Windows Server 2008 R2部署FTP FileZilla Server防火墙设置
有一台服务器,之前文件迁移少,现准备用FileZilla Server当FTP服务器,服务器系统是Windows Server 2008 R2,同样适用FileZilla Client连接服务器FTP ...
- shell 参数
转:http://hi.baidu.com/ipvsadm/item/489d9e16460195ddbe9042ee linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中s ...