pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30)
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 42Sample Output:
58 25 82 11 38 67 45 73 42
题意:给定n个数插入BST,并且插入数值后的BST结构已经确定,依据这个结构来推断BST上每个节点对应的数值,并且层序遍历BST。
思路:一开始的思路比较复杂,将n各数值从小到大排列,我先搜索出了BST上每个节点左右子树的节点个数。在此基础上就可以递推的确定每个节点对应的数值在数列上的位置。假设
已经确定某一个节点x对应的数值在数列上的位置pos,那么其节点x的左儿子的数值所在位置与pos的距离间隔就是左儿子的右子树节点个数(原因:比左儿子的数值大又比x的数值小,这些节点的数值当然都存储在左儿子的右子树上),从而可以推断左儿子的数值。右儿子的数值推断方法类似。
但其实不需要如此复杂,按照中序遍历的顺序就可以直接推断出各个节点上对应的数值。。。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 100+5
int n;
struct Node {
int l_child, r_child;
int key;
}node[N_MAX];
vector<int>vec;
pair<int, int>num[N_MAX];
/*
int dfs(int x) {//查询每个节点左右儿子的数量
int left_num=0, right_num=0;
if(node[x].l_child!=-1)left_num = dfs(node[x].l_child);
if (node[x].r_child != -1)right_num = dfs(node[x].r_child);
num[x] = make_pair(left_num, right_num);
return right_num + left_num+1;
} void dfs2(int x,int pos) {//节点x上的数值为vec[pos],考虑节点x与儿子节点的距离来推断儿子节点的位置
node[x].key = vec[pos];
int pos_left = pos - num[node[x].l_child].second - 1;
int pos_right = pos + num[node[x].r_child].first + 1;
if(node[x].l_child!=-1)dfs2(node[x].l_child, pos_left);
if(node[x].r_child!=-1)dfs2(node[x].r_child, pos_right);
}*/ int step = ;
void inorder(int x) {//其实按照中序遍历的顺序就可以依次确定每个节点的数值key
if(node[x].l_child!=-)inorder(node[x].l_child);
node[x].key = vec[step++];
if(node[x].r_child!=-)inorder(node[x].r_child);
} int output[N_MAX];
void bfs(int root) {
queue<int>que;
que.push(root);
int cnt = ;
while(!que.empty()) {
int x = que.front(); que.pop();
output[cnt++] = node[x].key;
if(node[x].l_child!=-)que.push(node[x].l_child);
if(node[x].r_child!=-)que.push(node[x].r_child);
}
} int main(){
while (scanf("%d",&n)!=EOF) {
for (int i = ; i < n;i++) {
int l, r; scanf("%d%d",&l,&r);
node[i].l_child = l, node[i].r_child = r;
}
vec.resize(n);
for (int i = ; i < n; i++)scanf("%d",&vec[i]);
sort(vec.begin(),vec.end());
//dfs(0);
//dfs2(0, num[0].first);
inorder();
bfs();
for (int i = ; i < n; i++)printf("%d%c",output[i],i+==n?'\n':' ');
}
return ;
}
pat 甲级 1099. Build A Binary Search Tree (30)的更多相关文章
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- 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分)
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甲级——A1099 Build A Binary Search Tree
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 ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
随机推荐
- ES6学习(二):函数的扩展
chapter07 函数的扩展 7.1 函数默认值 7.1.1 参数默认值简介 传统做法的弊端(||):如果传入的参数相等于(==)false的话,仍会被设为默认值,需要多加入一个if判断,比较麻烦. ...
- cf550C. Divisibility by Eight(结论)
题意 给出长度为$n$的字符串,判断是否能删除一些数后被$8$整除 Sol 神仙题啊Orz 结论: 若数字的后三位能被$8$整除,则该数字能被$8$整除 证明 设$x = 10000 * a_i + ...
- STMS传输队列中的请求状态一直是Running不能结束
通过STMS传输请求时,遇到了如下问题: STMS传输请求,不论等多久的时间,请求状态一直是running,不能结束.但检查传输的内容时,发现CHANGE REQUEST包含的内容已经传输到目标Cli ...
- MySQL存储引擎MyISAM与InnoDB的区别比较
使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都会看到. MySQL默认的存储引擎是MyISAM,其他常用的就是InnoDB了. 至于到底用哪种存储引擎比较好?这个问题 ...
- 科学计算库Numpy——数组生成
等差数组 使用np.arange()或np.linspace()生成元素是等差数列的数组. 以10为底的数组 使用np.logspace()生成元素是以10为底的数组. 数组扩展 使用np.meshg ...
- [BZOJ1208]宠物收养所(Splay)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- COGS:313. [POI2001] 和平委员会
313. [POI2001] 和平委员会 ★★☆ 输入文件:spo.in 输出文件:spo.out 评测插件时间限制:1 s 内存限制:128 MB 题目描述 根据宪法,Bytelan ...
- 【Kubernetes】资源列表
1.Kubernetes资源列表 https://www.cnblogs.com/linuxk/p/10436260.html
- day20 Django Models 操作,多表,多对多
1 Django models 获取数据的三种方式: 实践: viwes def business(request): v1 = models.Business.objects.all() v2 = ...
- SetUnhandledExceptionFilter
SetUnhandledExceptionFilter 设置未捕获异常处理 通常windows程序长时间运行,会发生各种问题,例如访问异常,内存溢出,堆栈破坏等. 这时候通常希望程序自己能增加处理,而 ...