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 ...
随机推荐
- AU3获取系统激活信息
If IsActivated() = False Then ;InstallProductKey($OSkey) ; installs a product key and also activates ...
- dubbo心跳机制 (3)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 二.consumer端心跳机制 //创建ExchangeClie ...
- 【转】如何知道DLL是32位还是64位
源地址:http://blog.csdn.net/skysword2/article/details/52588624
- LVS+OSPF 架构(转)
http://blog.51cto.com/pmghong/1399385 LVS 和 LVS+keepalived 这两种架构在平时听得多了,最近才接触到另外一个架构LVS+OSPF.这个架构实际上 ...
- Common operators to overload-c++运算符重载的标准语法(全)
Common operators to overload Most of the work in overloading operators is boiler-plate code. That is ...
- luogu3327 [SDOI2015]约数个数和
link 设\(d(x)\)表示x约数个数,给定n,m,\(\sum_{i=1}^n\sum_{j=1}^md(ij)\) 多组询问,1<=T<=50000,1<=N, M<= ...
- 「模拟赛20180406」膜树 prufer编码+概率
题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998 ...
- 使用js实现水波效果
使用到的工具:jQuery Ripples Plugin 下载安装 git clone https://github.com/sirxemic/jquery.ripples.git 引入jquery, ...
- tftp简单文件传输协议搭建
TFTP 简单文件传输协议 安装 sudo apt-get install tftp tftpd openbsd-inetd 需要tftp tftpd openbsd-ine ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...