1043. Is It a Binary Search Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. PAT1043:Is It a Binary Search Tree

    1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  2. 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 作者 ...

  3. 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 作者 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)

    简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...

随机推荐

  1. 简单的使用rabbitmq的例子

    1.创建了两个项目 : (1).spring_cloud_rabbitmq_send 消息发送者 (2).spring_cloud_rabbitmq_receive  消息接受者 2. 添加rabbi ...

  2. C#实现截图

    语言环境 框架: .NET Framework 3.5IDE: VS2013窗体A(主窗体) /// <summary>/// 点击弹出截屏窗体/// </summary>// ...

  3. Owin password

    一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorization(授权),而不是Authentication(认证). ...

  4. dubbo心跳机制 (2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 来看一下HeaderExchangeServer.this.getChannels():   1     p ...

  5. Android手游外挂入侵----寓攻于守,方能破敌

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 手游外挂入侵 随着各种爆款手游的风靡,目前手机游戏的占比用户已经形成一个巨大的市场,市场上你争我夺,有将PC版本移植到手机中,也有新模式手游的推出. ...

  6. Summer Holiday 强连通

    Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity i ...

  7. json解析(自动判断是jsonArray和jsonObject)

    因为想做一个接口自动化框架,已经实现了接口的访问和连接及获取接口返回的json数据,但json数据的解析是个麻烦的事情,所以写一个简单的版本记录一下.后续会进行优化,实现方法分离以及自动识别循环解析返 ...

  8. delay JS延迟执行

    window.onscroll = function () {    throttle(trrigerAdd,window);};function trrigerAdd(){    var $dHei ...

  9. sql开发技巧总结-1

    1.数据库分类 关系型 非关系型 2.sql语句分类 sql: ddl数据库定义语言  tpl事物处理语言 dcl数据控制语言  dml数据操作语言(insert delete update sele ...

  10. rest framework认证组件和django自带csrf组件区别详解

    使用 Django 中的 csrf 处理 Django中有一个django.middleware.csrf.CsrfViewMiddleware中间件提供了全局的csrf检查.它的原理是在<fo ...