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 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 42
Sample Output:
58 25 82 11 38 67 45 73 42
题目分析
已知二叉查找树的所有非叶子节点的子节点信息,求其层序序列
解题思路
思路 01
- 用二维数组存储所有节点关系,将二叉查找树节点任意序列升序排序即为中序序列
- 建树(左右指针)
- 中序序列打印模拟过程,设置所有节点值信息
- 借助队列,实现层序遍历
思路 02
- 用节点数组建树(类似静态数组)
- 模拟中序序列打印过程,设置所有节点值信息,并记录index(index=i节点的子节点index为2i+1,2i+2(root存放在0下标)),level记录节点所在层
- 用index和level对节点数组排序
- 打印节点数组,即为层序序列
Code
Code 01
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100;
int n,cnt=0,in[maxn],nds[maxn][2];
struct node {
int data;
node * left=NULL;
node * right=NULL;
node() {}
node(int _data):data(_data) {}
};
node * inOrder(int index) {
if(index==-1) {
return NULL;
}
node * root = new node();
root->left=inOrder(nds[index][0]);
root->data = in[cnt++];
root->right=inOrder(nds[index][1]);
return root;
}
void levelOrder(node * root){
queue<node*> q;
q.push(root);
int index = 0;
while(!q.empty()){
node * now = q.front();
q.pop();
printf("%d",now->data);
if(++index<n)printf(" ");
if(now->left!=NULL)q.push(now->left);
if(now->right!=NULL)q.push(now->right);
}
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
int f,r;
for(int i=0; i<n; i++) {
scanf("%d %d",&f, &r);
nds[i][0]=f;
nds[i][1]=r;
}
for(int i=0; i<n; i++) scanf("%d",&in[i]);
sort(in,in+n);
node * root = inOrder(0);
levelOrder(root);
return 0;
}
Code 02(最优)
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100;
int n,cnt=0,in[maxn];
struct node {
int data;
double index=-1; //最坏情况,每一层一个节点,index最大为2^100 ,测试使用long long也有两个测试点不通过
int left=-1;
int right=-1;
node() {}
node(int _data):data(_data) {}
node(int _left, int _right) {
left=_left;
right=_right;
}
}nds[maxn];
void inOrder(int root, double index) {
if(root==-1) {
return;
}
inOrder(nds[root].left,2*index+1);
nds[root].index=index;
nds[root].data=in[cnt++];
inOrder(nds[root].right,2*index+2);
}
bool cmp(node &n1,node &n2){
return n1.index<n2.index;
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
int f,r;
for(int i=0; i<n; i++) {
scanf("%d %d",&nds[i].left, &nds[i].right);
}
for(int i=0; i<n; i++) scanf("%d",&in[i]);
sort(in,in+n);
inOrder(0,0);
sort(nds,nds+n,cmp);
for(int i=0;i<n;i++){
if(i!=0)printf(" ");
printf("%d",nds[i].data);
}
return 0;
}
PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]的更多相关文章
- PAT Advanced 1064 Complete 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 (二叉搜索树)
本文同步发布在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 Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- 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 (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
随机推荐
- NIO 与 零拷贝
零拷贝介绍 零拷贝是网络编程的关键, 很多性能优化都需要零拷贝. 在 Java程序中, 常用的零拷贝方式有m(memory)map[内存映射] 和 sendFile.它们在OS中又是怎样的设计? NI ...
- SQL注入汇总(手注,盲注,报错注入,宽字节,二次编码,http头部){10.22、23 第二十四 二十五天}
首先什么是SQL注入: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. SQL注入有什么危害? 危害:数据泄露.脱库 ...
- a标签-伪类
a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #00FF00} /* 已访问的链接 */ a:hover {color: #FF00FF ...
- Python 中 unittest 框架加载测试用例的常用方法
unittest 当中为我们提供了许多加载用例的方法,这里说下常用的两种方法...推荐使用第二种 第一种加载测试用例的方法:使用加载器加载两个模块 需要把所有的模块加载到套件中 那么就可以自动的运行所 ...
- 一百零七、SAP的OO-ALV之一,新建程序
一.来带SE38模块,新建一个Z_TIANPAN_20190807_OOALV的本地程序 二.设置一个标题,点击对勾 三.选择保存为本地对象 我们下一篇来写创建屏幕
- Web前端开发CSS规范总结
作为Web前端开发必备语言,CSS为大家广为熟知,今天就跟大家分享下CSS规范总结,Web前端的小伙伴们看过来吧! CSS样式的权值(权重) 权值等级的定义 第一等:代表内联样式,如: style=” ...
- ACM-Checker Challenge
题目描述:Checker Challenge 1000(ms) 10000(kb) 20 / 90 Examine the 6x6 checkerboard below and note tha ...
- java课程之团队开发冲刺阶段2.6
总结昨天进度: 1.总体的思路已经完成,代码也差不多了,只剩下对闹钟activity的设置 遇到的困难: 1.在设置震动的时候,对方法有点不太理解,所以使用的时候产生了错误,没有达到预期的效果 今天的 ...
- MySQL新增数据,存在就更新,不存在就添加
1.插入一条数据,存在就更新,不存在就更新(必须现有唯一键)使用insert ignore语句: insert ignore into table(col1,col2) values ('a','b' ...
- 【机器学习实战学习笔记(1-2)】k-近邻算法应用实例python代码
文章目录 1.改进约会网站匹配效果 1.1 准备数据:从文本文件中解析数据 1.2 分析数据:使用Matplotlib创建散点图 1.3 准备数据:归一化特征 1.4 测试算法:作为完整程序验证分类器 ...