题目

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

  1. 输入测试数据时,分别建树和建镜像树
  2. 先用树的先序序列与原测试序列对比,若同即输出YES,若不同再用镜像树先序序列对比,若同输出YES,不同则NO
  3. 若为YES,打印相应后序序列

思路 02(最优、难理解)

  1. 输入测试数据时,建树
  2. 根据二叉查找树的性质(大于所有左子树节点,小于所有右子树节点)

    2.1 获取后序序列,若后序序列中的结点数与原测试用例结点数相同,即为二叉查找树的先序序列打印YES,若不同,清空,并进性镜像树的后序序列获取

    2.2 获取镜像树的后序序列结点数,若与原测试用例结点数相同,即为二叉查找树镜像树的先序序列YES,若不同,打印NO
  3. 若为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]的更多相关文章

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

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

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

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

  5. PAT 甲级 1043 Is It a Binary Search Tree

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

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

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

  8. PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)

    简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...

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

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

随机推荐

  1. VMWare 禁用vmem虚拟内存文件

    使用 VMWare 虚拟机,虚拟机启动后,会在虚拟机目录下建立一个与虚拟内存大小相同的 .vmem文件 这个文件主要是将虚拟机内存的内容映射到磁盘,以支持在虚拟机的暂停等功能 对所有的虚拟机" ...

  2. win10热键体验

    Alt+Tab: 横向显示正在执行的进程 Win+Tab: 3D形式展示正在执行的进程 Win+D:返回桌面(逃领导查电脑和放窥屏尴尬) Win+R: run(直接打开文件开始运行) crtl+Alt ...

  3. swarm docker-compose.yml NFS 搭建LNMP

    列表: 172.16.0.40     (swarm-master) 172.16.0.160     (swarm-worker) 172.16.0.170     (swarm-worker) 1 ...

  4. Thread start0 启动分析 一图看懂

    参考文章: https://segmentfault.com/a/1190000017255007 https://segmentfault.com/a/1190000020194154 1.线程启动 ...

  5. Origin中使用CopyPage复制图片到Word后比例失调解决办法

    Origin画图的优势很多,其图形美观易于操作.对我而言,Origin最大的优点就是与Word兼容,在Origin操作界面空白处直接使用右键CopyPage命令,然后在Word中使用粘贴命令即可插入图 ...

  6. Docker 搭建开源跳板机_jumpserver (运维开源堡垒机_jumpserver) Centos_7.0

    最近看到一个开源项目(jumpserver)  很不错  还是用Docker  部署得 ... 抽了点时间拿来学习一下  部署    分析   简单使用一下  ....好了先搭起来 准备 工作:    ...

  7. UVA - 1149 Bin Packing(装箱)(贪心)

    题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品.求至少要多少个背包才能装下所有的物品. 分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个 ...

  8. 现在购买5G“商用手机”值不值呢?是花冤枉钱还是提前享受?

    刚刚发布的"安卓最强机皇"华为Mate 20系列依然没有支持5G,只是在收割最后的4G红利.相比之下,即将发布的小米MIX 3,看起来亮点还是颇多的.除了滑盖摄像头.屏占比更高的全 ...

  9. 【LeetCode】最长连续序列

    [问题]给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [, , , , , ] 输出: 解释: 最长连续序列是 [, , , ].它的长度为 ...

  10. idea导入新项目后右键main方法没有Run xxx.main()

    刚安装的idea2019.1,基本上没有什么配置.导入从github clone下来的工程.鼠标右键main方法时没有发现 run 选项,如下图所示: 这里是配置有问题造成的,对比下正常的工程,可以发 ...