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. C#之WinForm界面分辨率问题

    在做上一个C#小工具的时候,当时为了处理界面最大化,分辨率问题,只是简单的用各种···Panle控价简单随意的处理控件的大小位置,字体什么的就随缘了(貌似有点不负责任啊,嘿嘿~). 所以在开始第二个C ...

  2. day06.2-软链接与硬链接

    1. 建立软链接:ln   -s   源文件   链接文件 特点:a). 相当于Windons系统中的快捷方式:        b). 删除链接文件不影响源文件内容,而删除源文件后链接文件随即失效: ...

  3. 常用SQL语句及在node中使用MySQL

    摘要:一些重要的SQL命令 SELECT - 从数据库中提取数据 UPDATE - 更新数据库中的数据 DELETE - 从数据库中删除数据 INSERT INTO - 向数据库中插入新数据 CREA ...

  4. C - 又见GCD

    有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c.  Input第一行输入一个n,表示有n组测试数据,接下来 ...

  5. C#之重载

    前言 今天去看了看论坛,发现有些人对于重载还是很有疑问的,像大多数人一样,貌似知道重载方法这回事儿, 但是具体怎么应用,或者用重载方法的好处,好像还是一知半解,模模糊糊.我们都知道,重载方法的定义,甚 ...

  6. Django个人博客开发 | 前言

    本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...

  7. [題解](最小生成樹)luogu_P2916安慰奶牛

    可以發現每個點經過次數恰好等於這個點的度數,所以把點權下放邊權,跑最小生成樹,原來邊權乘二在加上兩端點權,答案再加一遍起點最小點權 #include<bits/stdc++.h> #def ...

  8. java中list里面存放map,根据map中的某一个字段进行排序

    package com; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; ...

  9. python 对三维CT数据缩放

    项目需要对CT数据进行缩放,这里我存储CT数据的格式是numpy数组. 一共尝试了三种方法,分别是numpy.resize,cv2.resize,scipy.ndimage.interpolation ...

  10. JS模式和原型精解

    首先需要知道的是 模式只是思想.不要用 结构 看模式. ES中函数是对象,因此函数也有属性和方法. 每个函数含有两个属性: length 和 prototype 每个函数含有两个非继承的方法: app ...