pat1043. 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 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 (<=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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
struct node
{
int v;
node *l,*r;
node()
{
l=r=NULL;
}
};
bool Buildtree(node *&h,int *line,int n)
{
if(n==)
{
return true;
}
int i;
h=new node();
h->v=line[];
i=;
while(i<n&&line[i]<line[])
{
i++;
}
int j=i;
while(j<n&&line[j]>=line[]){
j++;
}
if(j!=n){
return false;
}
return Buildtree(h->l,line+,i-)&&Buildtree(h->r,line+i,n-i);
} bool Buildtree1(node *&h,int *line,int n)
{
if(n==)
{
return true;
}
int i;
h=new node();
h->v=line[];
i=;
while(i<n&&line[i]>=line[])
{
i++;
}
int j=i;
while(j<n&&line[j]<line[]){
j++;
}
if(j!=n){
return false;
}
return Buildtree1(h->l,line+,i-)&&Buildtree1(h->r,line+i,n-i);//黏贴复制害死人
}
void Postorder(node *&h)
{
if(h)
{
Postorder(h->l);
Postorder(h->r);
printf("%d ",h->v);
delete []h;
}
}
int line[];
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF)
{
node *h;
int i;
for(i=; i<n; i++)
{
scanf("%d",&line[i]);
}
if(n==){
printf("YES\n");
printf("%d\n",line[]);
continue;
}
if(line[]>line[]&&Buildtree(h,line,n))//BST
{
printf("YES\n");
Postorder(h->l);
Postorder(h->r);
printf("%d\n",h->v);
continue;
}
if(line[]<=line[]&&Buildtree1(h,line,n))
{
printf("YES\n");
Postorder(h->l);
Postorder(h->r);
printf("%d\n",h->v);
continue;
}
printf("NO\n");
}
return ;
}
pat1043. Is It a Binary Search Tree (25)的更多相关文章
- PAT1043:Is It a Binary Search Tree
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 作者 ...
- 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 ...
- PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 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 ...
- 1043. Is It a Binary Search Tree (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
随机推荐
- MVC下为什么要使用Areas
想研究一下这个Areas,在博客园知识库找到这篇文章,先全部搬过来吧,原文地址:http://kb.cnblogs.com/page/144561/ 为什么需要分离? 我们知道MVC项目各部分职责比较 ...
- WeChat 微信公众号开发步骤
WeChat 微信公众号开发步骤 一.什么是微信公众号? 微信公众号是开发者或商家在微信公众平台上申请的应用账号,该帐号与QQ账号互通,通过公众号,商家可在微信平台上实现和特定群体的文字.图片.语音 ...
- 第一个HelloWorld!
$.介绍 1.eclipse的基本使用 2.第一个程序HelloWorld 3.总结 $.基本使用 对于刚入门的java新手来说选择一个舒适的编译器能让你快速的上手java的程序编写. 针对英语low ...
- 搭建TensorFlow
网上有许多在线安装TensorFlow框架的,我试了好多,结果安装时间长先不说,还总是出现一些问题,然后我就想着离线安装,成功了,与大家分享! (1)首先,需要下载离线安装的TensorFlow包,可 ...
- 手把手教你如何制作和使用lib和dll_转载
目录 静态库 什么是静态库? 怎么创建 如何使用 静态库的第一种使用方法 静态库的第二种使用方法 动态链接库 动态库是什么? 怎么创建 如何使用 隐式调用 显式调用 静态库什么是静态库?我们先来说一下 ...
- Liunx基础优化配置
1: 为系统添加操作用户,并授予sudo权限 [root@localhost ~]# groupadd cai [root@localhost ~]# useradd cai -g cai [roo ...
- [国家集训队]happiness 最小割 BZOJ 2127
题目描述 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友如果能同时选文 ...
- idea 新建maven项目没有src及其子目录问题
注意在这一步中,填写maven的本地地址还有手动修改settings地址非常重要!!! 如果你是第一次配置maven,少配置任何一个将导致你以后建立的mvn项目全部没有src目录!!! 解决办法就是卸 ...
- Gson的fromJson()方法
Gson提供了fromJson()方法来实现从Json相关对象到Java实体的方法. 在日常应用中,我们一般都会碰到两种情况,转成单一实体对象和转换成对象列表或者其他结构. 先来看第一种: 比如jso ...
- [转载]什么情况下应该设置 cudnn.benchmark = True?
总的来说,大部分情况下,设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题. 一般来讲,应该遵循以下准则: 如果网络的输 ...