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 ...
随机推荐
- 领扣-两数之和-Python实现
领扣每日一题 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- ACM1019:Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- 20155310 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告
20155310 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 •初步掌握单元测试和TDD •理解并掌握面向对象三要素:封装.继承.多态 •初步掌握UML建模 ...
- MySQL优化Explain命令简介(二)
type列 MySQL手册上注明type列用于描述join type,不过我们认为把这一列视为对access type--即MySQL决定如何在表中寻找数据的方式的描述,更加合适一些,以下所示从最坏情 ...
- (转) 理解Angular中的$apply()以及$digest()
原文地址:http://blog.csdn.net/dm_vincent/article/details/38705099 $apply()和$digest()在AngularJS中是两个核心概念,但 ...
- 微服务介绍及Asp.net Core实战项目系列之微服务介绍
0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.微服务选型 在做微服务架构的技术选型的时候,我们以“无侵入”和“社区活跃”为主要的考量点,将来升级为原子服务架构.量子服务架构 ...
- Selenide 阶段性总结介绍(UI自动化测试工具)
今天给大家介绍一个比较新的UI自动化测试工具-- Selenide.确实是比较新的,国内应该还没有多少人用它.在百度和google上你只能搜到一个中文帖子简单介绍了一下.如果你想用这个工具,不可避免的 ...
- python登录验证码生成及自动化测试规避
在用django写论坛的时候,需要有登录及注册功能. 故就登录界面后端需要生成随机验证码并传值给前端的代码进行编写如下. 验证码生成png需要调用到python的图形库 生成注册码img import ...
- Mysql基础操作语句
SQL 简单的增删改查 不区分大小写, 表名和字段名可不加引号 查询语句 SELECT * FROM `table_name`; -- 注释 CTRL+/ : 注释 CTRL+/ : 取消注释 /* ...
- mysql 数据库优化之执行计划(explain)简析
数据库优化是一个比较宽泛的概念,涵盖范围较广.大的层面涉及分布式主从.分库.分表等:小的层面包括连接池使用.复杂查询与简单查询的选择及是否在应用中做数据整合等:具体到sql语句执行效率则需调整相应查询 ...