A1043 Is It a Binary Search Tree (25 分)
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.
If we swap the left 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 (≤). 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
#include<cstdio>
struct node{
int data;
node *lchild;
node *rchild;
};
node *newNode(int x){//生成新的节点
node *rt=new node;
rt->data=x;
rt->lchild=rt->rchild=NULL;
return rt;
}
void insert(node* &rt,int x){
if(rt==NULL){//在BST树中插入节点
rt=newNode(x);
return;
}
if(rt->data>x){
insert(rt->lchild,x);
}else{
insert(rt->rchild,x);
}
}
node *create(int data[],int n){//创建BST树
node *rt=NULL;
for(int i=;i<n;i++){
insert(rt,data[i]);
}
return rt;
}
node *swapBst(node *rt){//利用前序递归遍历,实现将所有节点的左子树
if(rt==NULL){//和右子树相互交换
return NULL;
}else{
node *temp=rt->lchild;
rt->lchild=rt->rchild;
rt->rchild=temp;
swapBst(rt->lchild);
swapBst(rt->rchild);
}
return rt;
}
int* preOrder(node *rt,int pre[],int &k){
if(rt==NULL){//将BST树遍历输出到指定数组中
return NULL;
}else{
pre[k++]=rt->data;
preOrder(rt->lchild,pre,k);
preOrder(rt->rchild,pre,k);
}
return pre;
}
bool isP(int data[],int temp[],int n){
for(int i=;i<n;i++){//判定两个数组中元素是否对应相等
if(data[i]!=temp[i]){
return false;
}
}
return true;
}
void postOrder(node *rt,int &n){//后序遍历输出二叉树
if(rt==NULL){
return;
}else{
postOrder(rt->lchild,n);
postOrder(rt->rchild,n);
if(n!=){
printf("%d ",rt->data);
n--;
}else{
printf("%d",rt->data);
}
}
}
int main()
{
int n;
int data[];
int pre[];
int mir[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&data[i]);
}
node *rt=create(data,n);
int k=;
preOrder(rt,pre,k);
if(isP(data,pre,n)){
printf("YES\n");
postOrder(rt,n);
return ;
}
node *mirr=swapBst(rt);
k=;
preOrder(mirr,mir,k);
if(isP(data,mir,n)){
printf("YES\n");
postOrder(rt,n);
return ;
}
printf("NO\n");
return ;
}
Mist Note:本题考察二叉搜索树(BST)的相关算法,首先要学会利用给定序列建立BST,建立之后会递归遍历,
灵活利用递归遍历交换所有节点的左右子树。
A1043 Is It a Binary Search Tree (25 分)的更多相关文章
- 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 ...
- 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 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 ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- 04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
随机推荐
- 1、kvm的vnc服务关闭、设置网络模式
一.kvm的vnc服务关闭 1.关闭虚拟机 virsh shutdown privi-server 2.virsh edit privi-server找到下面内容进行删除 <graphics t ...
- Flask&&人工智能AI --4
一.flask请求上下文源码解读 通过上篇源码分析,我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__call__方法返回了app的wsgi_app(en ...
- excel无法复制
空字符(ascii 码 0,在程序里一般写作"\0"),在 vim 里就显示成 ^@. 如果在 vim 里遇到不确定的口字苻,可以在那个字符上按 ga(普通模式下宜接按,先 g 再 ...
- QT 商业版调用activex插件
搭建好qt项目后 在main.cpp中写入如下代码 #include "test.h" #include <QtWidgets/QApplication> #inclu ...
- Zookeeper启动失败:java.net.BindException: Address already in use
错误日志如下: [hadoop@master zookeeper-3.4.5-cdh5.10.0]$ cat zookeeper.out 2018-05-15 01:29:21,036 [myid:] ...
- 【Java】在eclipse中使用maven进行项目构建 入门篇
maven配置的简单说明 从\192.168.30.150\103.初级人员培训资料\新建文件夹 (2)\环境下提取apache-maven-3.0.4.zip压缩包,解压缩至E盘下 在E盘下新建&q ...
- AOP的XML实现方式
与注解方式类似,只不过所有设置是通过xml来设置 // 切面类 public class Aop { public void around(ProceedingJoinPoint pjp) throw ...
- spring mvc 注解扫描问题 ,扫描不到controller, use-default-filters="false"
今天搭了个spring mvc项目,怎么也扫描不到controller,最后发现问题在use-default-filters="false"上面,乱copy出的问题 (默认值是tr ...
- 学习笔记:《JavaScript高级程序设计》
第1章 JavaScript简介 1.一个完整的JavaScript实现应该由三部分组成:核心(ECMAScript),文档对象模型(DOM)和浏览器对象模型(BOM). 2.Web浏览器只是ECMA ...
- 企业常用的站内收索、QQ群、在线客服
<div class="toplinks"> <form target="_blank"> ...