题目

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

  1. 用二维数组存储所有节点关系,将二叉查找树节点任意序列升序排序即为中序序列
  2. 建树(左右指针)
  3. 中序序列打印模拟过程,设置所有节点值信息
  4. 借助队列,实现层序遍历

思路 02

  1. 用节点数组建树(类似静态数组)
  2. 模拟中序序列打印过程,设置所有节点值信息,并记录index(index=i节点的子节点index为2i+1,2i+2(root存放在0下标)),level记录节点所在层
  3. 用index和level对节点数组排序
  4. 打印节点数组,即为层序序列

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]的更多相关文章

  1. 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 ...

  2. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  3. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  4. 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 ...

  5. 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 ...

  6. PAT 甲级 1099 Build A Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...

  7. 1099. Build A Binary Search Tree (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  8. PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

    预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...

  9. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

随机推荐

  1. java分词技术(自动提取关键词,段落大意)hanlp

    这是老师大作业需要的技术才知道hanlp这个外部包  使用方法 包以及数据如下GIT下载很慢我传百度云了   链接:https://pan.baidu.com/s/14a22v1g_CAQN-G-k8 ...

  2. WC2020 联训 #19 矩阵

    好不容易自己切一道题 链接 Description 在一个 \(n×(n+1)\) 的棋盘上放棋子, \(n\) 行中每行都恰好有两枚棋子,并且 \(n+1\) 列中每列都至多有两枚棋子,设 \(n= ...

  3. Json实体类驼峰名称转化器

    背景 我们常用一些网站,将json转化成实体类.但不巧的是,这些自动生成的都是小驼峰.需要进一步的改成大驼峰+JsonProperty.接着同事说他已经有个工具了.我稍微简化了一下 方法 首先行分离. ...

  4. Shell脚本之awk篇

    目录:一.概述二.awk基本语法格式三.awk基本操作四.awk条件及循环语句五.awk函数六.awk演示示例(源自于man手册) 一.概述 1. 产品概述: awk是一种编程语言,用于在linux/ ...

  5. 锤子科技向OpenBSD基金会捐款195 万

    导读 专注于提供 OpenBSD 资讯的网站 OpenBSD Journal 昨日报道了锤子科技成为 OpenBSD 基金会 2019 年首位铱金捐赠者的消息. 根据 OpenBSD Journal ...

  6. 侯捷C++学习(二)

    #include <iostream>using namespace std;class complex{ public: complex (double r= 0, double i = ...

  7. 在远程服务器上执行本地的shell脚本

    在远程服务器上执行本地的shell脚本 [root@localhost zzx]# sh echoip.sh 192.168.67.131[root@localhost zzx]# ssh root@ ...

  8. python 鞍点

    # 鞍点: 所在行的最大值,所在列的最小值 import random A = [[random.randint(1,100) for j in range(5)]for i in range(5)] ...

  9. 一百一十、SAP的OO-ALV之四,定义屏幕相关变量和逻辑流

    一.代码如下,定义相关变量 二.来带屏幕页面,双击STATUS_9000和USER_COMMAND_9000,自动生成相应代码 三.点击是 四.会自动生产关联的Includ文件 五.我们自己创建一个M ...

  10. 140-PHP类的抽象方法和继承

    <?php abstract class father{ //定义一个抽象类 abstract public function test(); //定义抽象方法 } class son exte ...