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 properties: The lef 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 lef and right subtrees must also be binary search trees. If we swap the lef and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST. Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line “YES” if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or “NO” if not. Then if the answer is “YES”, print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO
题目分析
已知二叉查找树节点序列,判断是其前序序列还是其镜像树的前序序列,并打印相应树的后序序列
解题思路
思路 01
- 输入测试数据时,分别建树和建镜像树
- 先用树的先序序列与原测试序列对比,若同即输出YES,若不同再用镜像树先序序列对比,若同输出YES,不同则NO
- 若为YES,打印相应后序序列
思路 02(最优、难理解)
- 输入测试数据时,建树
- 根据二叉查找树的性质(大于所有左子树节点,小于所有右子树节点)
2.1 获取后序序列,若后序序列中的结点数与原测试用例结点数相同,即为二叉查找树的先序序列打印YES,若不同,清空,并进性镜像树的后序序列获取
2.2 获取镜像树的后序序列结点数,若与原测试用例结点数相同,即为二叉查找树镜像树的先序序列YES,若不同,打印NO - 若为YES,打印相应后序序列
知识点
二叉查找树的前序转后序,无需建树,可根据其性质(大于所有左子树节点,小于所有右子树节点)建树
如前序序列:8 6 5 7 10 8 11
8是根节点
左子树:从6开始往后找小于8的都为8的左子树节点
右子树:从最后一位11开始往前找大于8的都为8的右子树节点
继续递归过程,直到完成建树
Code
Code 01
#include <iostream>
#include <vector>
using namespace std;
struct node {
int data;
node * left=NULL;
node * right=NULL;
node() {}
node(int _data):data(_data) {}
};
node * root,* rootM;
void insert(int n, int b) {
if(root==NULL&&b==0) {
root = new node(n);
return;
}
if(rootM==NULL&&b==1) {
rootM = new node(n);
return;
}
node * p;
if(b==0)p=root;
else p=rootM;
while(p!=NULL) {
if((n<p->data&&b==0)||(n>=p->data&&b==1)) {
if(p->left==NULL) {
p->left=new node(n);
return;
}
p=p->left;
} else if((n>=p->data&&b==0)||(n<p->data&&b==1)) {
if(p->right==NULL) {
p->right=new node(n);
return;
}
p=p->right;
}
}
}
vector<int> origin,pre,post,preM,postM;
void preOrder(node * nd, int b) {
if(nd==NULL)return;
if(b==0)pre.push_back(nd->data);
else preM.push_back(nd->data);
preOrder(nd->left,b);
preOrder(nd->right,b);
}
void postOrder(node * nd, int b) {
if(nd==NULL)return;
postOrder(nd->left,b);
postOrder(nd->right,b);
if(b==0)post.push_back(nd->data);
else postM.push_back(nd->data);
}
int main(int argc,char * argv[]) {
int n,m;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&m);
origin.push_back(m);
insert(m,0);
insert(m,1);
}
// int flag = -1;//0 前序;1 镜像前序;2 NO
preOrder(root,0);
preOrder(rootM,1);
if(pre==origin) {
postOrder(root,0);
printf("YES\n");
for(int i=0; i<post.size(); i++) {
if(i!=0)printf(" ");
printf("%d",post[i]);
}
} else if(preM==origin) {
if(preM==origin) {
postOrder(rootM,1);
printf("YES\n");
for(int i=0; i<postM.size(); i++) {
if(i!=0)printf(" ");
printf("%d",postM[i]);
}
}
}else{
printf("NO\n");
}
return 0;
}
Code 02(最优、难理解)
#include <iostream>
#include <vector>
using namespace std;
vector<int> pre,post;
bool isMirror;
void getPost(int root, int tail) {
if(root>tail)return;
int i=root+1;
int j=tail;
if(!isMirror) {
while(i<=tail&&pre[i]<pre[root])i++;
while(j>root&&pre[j]>=pre[root])j--;
} else {
while(i<=tail&&pre[i]>=pre[root])i++;
while(j>root&&pre[j]<pre[root])j--;
}
if(i-j!=1)return;
getPost(root+1,j);//左子树
getPost(i,tail); //右子树
post.push_back(pre[root]);
}
int main(int argc,char * argv[]) {
int n,m;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&m);
pre.push_back(m);
}
getPost(0,n-1);
if(post.size()!=n) {
isMirror=true;
post.clear();
getPost(0,n-1);
}
if(post.size()==n) {
printf("YES\n%d",post[0]);
for(int i=1; i<post.size(); i++) {
printf(" %d",post[i]);
}
} else {
printf("NO\n");
}
return 0;
}
PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]的更多相关文章
- 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 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 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- 【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1043 Is It a Binary Search Tree (25分)(树的插入)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
随机推荐
- windows搭建安装react-native环境
在win10环境下,利用Genymotion模拟器,配置react-native的环境. 一.安装JDK 在网上下载jdk,版本最好是1.8以上.安装后要对环境变量进行配置. 同时在 Path 中配置 ...
- GDOI#345. 送礼物「JSOI 2015」01分数规划+RMQ
题目描述 JYY和CX的结婚纪念日即将到来,JYY来到萌萌开的礼品店选购纪念礼物.萌萌的礼品店很神奇,所有出售的礼物都按照特定的顺序都排成一列,而且相邻的礼物之间有一种神秘的美感.于是,JYY决定从中 ...
- linux桌面系统 镜像下载
1.Ubuntu 官方下载地址(不推荐,网速较慢):https://www.ubuntu.com/download 阿里云:http://mirrors.aliyun.com/ubuntu-relea ...
- Golang函数-不定参函数
Golang函数-不定参函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- SVPWM总结
空间矢量算法 是以逆变器和电机作为一个整体来研究的.目标是产生电机定子的圆形磁场 模态选择, 上管导通 状态为1 下管导通 状态为0 那么状态为000 001 010 011 100 101 110 ...
- cf 782# A.Andryusha and Socks B.The Meeting Place Cannot Be Changed C.Andryusha and Colored Balloons
看来快掉到灰名的蒟蒻涨rating也快... A题模拟一下就好(一开始还sb,, #include<bits/stdc++.h> #define LL long long using na ...
- 二十二、SAP中创建一个内表,并添加内容循环输出显示
一.直接上代码 二.输出如下
- 移动端触屏click点击事件延迟问题,以及tap的解决方案
在移动端 触屏click事件虽然也会响应,但是总感觉是有延迟,一直听说click事件在手机上有200~300毫秒的延迟问题,亲自测了一下,在pc端模拟手机的话是测不出来的,但是用手机测试时发现延迟非常 ...
- Meeloun教你如何正式切入Essay写作话题
很多同学在Essay写作过程中会发现:如果题目问到解决办法,写来写去,都是政府要颁布政策,人们要提高意识,感觉一点新意也没有.怎么样更好地切合不同的话题,想到最合适的解决办法呢?今天小编为你奉上更多处 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 重载运算符和重载函数
C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不 ...