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 ...
随机推荐
- python3——“->”的含义
->:标记返回函数注释,信息作为.__annotations__属性提供,__annotations__属性是字典.键return是用于在箭头后检索值的键.但是在Python中3.5,PEP 4 ...
- A -- A. Quailty and Playing Cards 模拟 + 思考
http://www.ifrog.cc/acm/problem/1036?contest=1005&no=0 分类,考虑Q神出的是第一张或者是第二张,然后对手出那些牌来应付. Q神出第一张和第 ...
- 将本地代码添加到github
首先在github上创建一个仓库. 第一步:建立本地仓库 git init 关联远程仓库 git remote add origin https://github.com/tshua/***.git ...
- Java 内存模型(一)
打算花比较长的篇幅来描述下自己理解的JVM,尽量描述的清晰易懂一些,从简单慢慢到慢慢深入,一方面自己也复习一下,一方面也供大家参考,少走些弯路.鉴于本人水平有限,如有错误的地方,欢迎指出,感谢. 一段 ...
- Unity C# 调用SaveFileDialog保存Excel文件
本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6893934.html 本文学习如何把数据转存为Excel文件并调用SaveFileDial ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十四天(非原创)
文章大纲 一.淘淘商城总体架构介绍二.淘淘商城重要技术点总结三.项目常见面试题四.项目学习(all)资源下载五.参考文章 一.淘淘商城总体架构介绍 1. 功能架构 2. 技术选型 (1)Sprin ...
- vue3.0学习笔记(二)
一.选择合适的ide 推荐使用vs code编辑器,界面清晰.使用方便,控制台功能很好用.webstorm也可以,看个人喜好. 二.ui框架选择 目前,pc端一般是选择element ui(饿了么), ...
- Linux下安装JDK及相关配置
1.官网下载JDK:选择Linux压缩包进行下载 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...
- AngularJS(十):依赖注入
本文也同步发表在我的公众号“我的天空” 依赖注入 依赖注入不是AngularJS独有的概念,而是现代软件开发与架构的范畴,但是在AngularJS中“依赖注入”是其核心思想之一,所以我们专门来学习一下 ...
- 移动端,点击a标签链接的pdf报错 Resource interpreted as Document but transferred with MIME type application/pdf
源码: <a href="11.pdf" class="actcont_a fl report_a" style="display: block ...