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 ...
随机推荐
- css奇技淫巧之—多列等高
什么是等高布局? 先来看一个案例: 上图中的页面的主体内容是两列结构,左列是用来导航的,右列是用来显示内容的.我们看到它们有一个共同的边框,中间还有一条分隔线,左右两列的高度都是不固定的.这种情况下就 ...
- 借鉴redux,实现一个react状态管理方案
react状态管理方案有很多,其中最简单的最常用的是redux. redux实现 redux做状态管理,是利用reducer和action实现的state的更新. 如果想要用redux,需要几个步骤 ...
- Maven的安装以及介绍
附录:带阿里源的maven用户设置文件-settings.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- Javascript常见问题
倒计时 <Script Language="JavaScript"> var timedate= new Date("October 1,2002" ...
- Backbone源码解析系列
01 编码风格.继承 02 Backbone.Events 03 Backbone.Model 04 Backbone.View 05 Backbone.Router 06 Backbone应用于we ...
- linux服务器安装nodejs运行环境
安装nodejs运行环境 第一步:到node官网下载相应版本的安装包,将安装包放置服务器上,路径为 usr/local/node(可根据自身情况进行修改) 第二步:解压 ***.tar.xz格式文件需 ...
- Windows8 64位运行Silverlight程序不能访问WCF的解决方案
公司的项目是Silverlight+WCF,而我的本本是Win8 64位系统,一直无法正常运行Silverlight程序,一个同事找到了方案,现分享出来 一种情况是,Vs2010运行程序时,报无法加载 ...
- 学习笔记:Web Storage API
Web Storage API 提供了存储机制,通过该机制,浏览器可以安全地存储键值对,比使用 cookie 更加直观. Web Storage 包含如下两种机制: sessionStorage 为每 ...
- 学习笔记:MDN的Web入门
HTML: 要引用一个父目录的文件,加上两个点. HTML并不是真正的编程语言,它是一种用于定义内容结构的标记语言. 元素(Element):开标签.闭标签与内容相结合,便是一个完整的元素.元素可以用 ...
- Django的Serializers的使用
Serializer 在这里通过一个验证用户身份的例子说明rest_framework中serializer.Serialize的使用. 编写serializer Serializer的使用不需要依赖 ...