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 ...
随机推荐
- c# 手动实现 \u 转义字符。。效果。。。
string s ="\\u"+item.Icon; // item.Icon = UnicodeEncoding.Unicode.GetString(UnicodeEncodin ...
- 牛客假日团队赛1 J.分组
链接: https://ac.nowcoder.com/acm/contest/918/J 题意: 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想 ...
- PHP根据经纬度计算距离
思路: 公式: W为纬度对应的弧度,J为经度对应的弧度,如上图所示 下面代码 lat是纬度 lng是经度 /** * 根据经纬度算距离,返回结果单位是公里,先纬度,后经度 * @param $la ...
- 机器学习框架ML.NET学习笔记【4】多元分类之手写数字识别
一.问题与解决方案 通过多元分类算法进行手写数字识别,手写数字的图片分辨率为8*8的灰度图片.已经预先进行过处理,读取了各像素点的灰度值,并进行了标记. 其中第0列是序号(不参与运算).1-64列是像 ...
- sql server sql语句
添加联合唯一索引 create unique index 索引名 on 表名(列名1,列名2……)
- 白话SpringCloud | 第二章:服务注册与发现(Eureka)-上
前言 从本章节开始,正式进入SpringCloud的基础教程.从第一章<什么是SpringCloud>中我们可以知道,一个微服务框架覆盖的东西是很多的,而如何去管理这些服务或者说API接口 ...
- mysql存储方式MyISAM 和 InnoDB的区别
MyISAM 和 InnoDB 讲解: InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级 ...
- 禁止form重复提交
$("form").submit(function () { console.log("提交了"); $("input:submit").a ...
- 前端三剑客之javascript
前端三剑客之javascript 给个小目录 一.JavaScript介绍 二.ECMAScript(核心) 三.BOM对象(浏览器对象) 四.DOM对象(文档对象模型) 总结: JS的组成: a ...
- 移动端真机调试工具--DebugGap (VIDE)
越来越多的移动端开发工作,需要我们有一个好的调试工具,以解决各类真机才会遇到的BUG,最近使用了一款DebugGap 的工具,非常方便,在这里推荐给大家. 官网地址 DebugGap . 按需求下载 ...