pat1043. Is It a Binary Search Tree (25)

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

提交代码

 

 

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<queue>
  6 #include<vector>
  7 #include<cmath>
  8 #include<string>
  9 #include<map>
 10 #include<set>
 11 using namespace std;
 12 struct node
 13 {
 14     int v;
 15     node *l,*r;
 16     node()
 17     {
 18         l=r=NULL;
 19     }
 20 };
 21 bool Buildtree(node *&h,int *line,int n)
 22 {
 23     if(n==0)
 24     {
 25         return true;
 26     }
 27     int i;
 28     h=new node();
 29     h->v=line[0];
 30     i=1;
 31     while(i<n&&line[i]<line[0])
 32     {
 33         i++;
 34     }
 35     int j=i;
 36     while(j<n&&line[j]>=line[0]){
 37         j++;
 38     }
 39     if(j!=n){
 40         return false;
 41     }
 42     return Buildtree(h->l,line+1,i-1)&&Buildtree(h->r,line+i,n-i);
 43 }
 44 
 45 bool Buildtree1(node *&h,int *line,int n)
 46 {
 47     if(n==0)
 48     {
 49         return true;
 50     }
 51     int i;
 52     h=new node();
 53     h->v=line[0];
 54     i=1;
 55     while(i<n&&line[i]>=line[0])
 56     {
 57         i++;
 58     }
 59     int j=i;
 60     while(j<n&&line[j]<line[0]){
 61         j++;
 62     }
 63     if(j!=n){
 64         return false;
 65     }
 66     return Buildtree1(h->l,line+1,i-1)&&Buildtree1(h->r,line+i,n-i);//黏贴复制害死人
 67 }
 68 void Postorder(node *&h)
 69 {
 70     if(h)
 71     {
 72         Postorder(h->l);
 73         Postorder(h->r);
 74         printf("%d ",h->v);
 75         delete []h;
 76     }
 77 }
 78 int line[1005];
 79 int main()
 80 {
 81     //freopen("D:\\INPUT.txt","r",stdin);
 82     int n;
 83     while(scanf("%d",&n)!=EOF)
 84     {
 85         node *h;
 86         int i;
 87         for(i=0; i<n; i++)
 88         {
 89             scanf("%d",&line[i]);
 90         }
 91         if(n==1){
 92             printf("YES\n");
 93             printf("%d\n",line[0]);
 94             continue;
 95         }
 96         if(line[0]>line[1]&&Buildtree(h,line,n))//BST
 97         {
 98             printf("YES\n");
 99             Postorder(h->l);
100             Postorder(h->r);
101             printf("%d\n",h->v);
102             continue;
103         }
104         if(line[0]<=line[1]&&Buildtree1(h,line,n))
105         {
106             printf("YES\n");
107             Postorder(h->l);
108             Postorder(h->r);
109             printf("%d\n",h->v);
110             continue;
111         }
112         printf("NO\n");
113     }
114     return 0;
115 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4770374.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/258408.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微软待办应用更新

微软做了一些更改和优化来改进微软待办。 为了在所有设备上获得最佳体验&#xff0c;需确保移动和桌面微软待办2021 年 12 月 31日之前的版本为 2.49 或更高版本&#xff0c;否则微软待办不再支持跨设备同步&#xff0c;但仍然能脱机使用。 桌面版的微软待办应用下载地址为&…

出租WiFi到底靠不靠谱?

创业是一种心态&#xff0c;也是不断的探索&#xff0c;他融入我们的生活&#xff0c;从日常中积累&#xff0c;从小微处启航。 一、背景交代 最近在换工作&#xff0c;本周搬到新租的单身公寓&#xff0c;空间不大&#xff0c;倒是干净整洁。委托租房中介帮忙开通宽带&#xf…

AD20学习笔记1---元件库的创建

前言&#xff1a; 本文学习视频是B站点击率第一的凡亿教育《Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设计视频教程》&#xff0c;视频地址&#xff1a;Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设…

nodejs环境搭建与express安装配置

一、NPM 1、下载nodeJS 下载地址&#xff1a;https://nodejs.org/en/download/ 因为我的系统是Linux 的&#xff0c;所以下载已经编译好的Linux&#xff0c;nodejs tar包 3、下载完成过后放到/usr/local/下面 4、解压&#xff1a;因为这个包不是gz的包所以解压 正确&#xff1a…

在vue中实现picker样式_基于Vue实现timepicker

主要用到的还是Vue的基本知识而已&#xff0c;不过要想到的细节很多。先放效果&#xff0c;点击上框&#xff0c;显示timepicker。而且可以根据点击的是时还是分来改变圆盘的数字。这里我用了两个组件&#xff0c;和&#xff0c;这里的时和分的数值我挂在了根实例中&#xff0c…

玩玩

金字塔一样输出字母&#xff0c;如 输入 d a a b a a b c b a a b c d c b a 代码实现 #include<stdio.h> int main(void) { char z; int j,t,k; scanf("%c",&z); t0; if(z>a&&z<z) { for(int i0;i<z-a;i) { for(kz-a-t;k…

总结界面框架_UI_Adapter

本人定期更新经典案例及解决方案如有疑问请联系我QQ1822282728 -- 277627117 下面是常用到的ui Demo安卓三级筛选菜单listview&#xff08;非常经典&#xff09; http://download.csdn.net/detail/zillvip/9138975android地图应用&#xff08;路径规划&#xff0c;地理编码&…

AD20学习笔记2---原理图绘制及编译检查

前言&#xff1a; 本文学习视频是B站点击率第一的凡亿教育《Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设计视频教程》&#xff0c;视频地址&#xff1a;Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设…

git如何设置master分支的权限_Git 从master 分支拉新分支开发

一、 切换到被copy的分支(master)&#xff0c;并且从远端拉取最新版本$git checkout master$git pull二、从当前分支拉copy开发分支$git checkout -b devSwitched to a new branch dev三、 把新建的分支push到远端$git push origin dev四、拉取远端分支$git pullThere is no tr…

Yii框架 phpexcel 导出

一、说明 之前使用的是PHPExcelXML包实现的数据导出&#xff0c;由于导出的文件扩展名为“.xls” 在office2007上带不开&#xff0c;报如下图错误&#xff08;用 WPS都能打开&#xff09; 因此&#xff0c;此次采用了 PHPExcel包 不仅支持生成Excel&#xff08;.xls&#xff09…

慎用stl中的erase的返回值

在windows下的VC编译或者Mac OX的XCode下编译也许不会出问题。但是在linux下可能就会挂掉。 比如我上一篇里的poj4093出现了编译错误 2007120.8890/Main.cc: In function ‘int main()’: 2007120.8890/Main.cc:50:44: error: no match for ‘operator’ in ‘itr1 a.std::set…

AD20学习笔记3---PCB封装库的创建方法及现有封装调用

前言&#xff1a; 本文学习视频是B站点击率第一的凡亿教育《Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设计视频教程》&#xff0c;视频地址&#xff1a;Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设…

php的两种复合数据类型是什么意思_2.4PHP复合数据类型:数组和对象

Posted by 撒得一地 on 2015年9月29日 in PHP入门教程国外稳定加速器推荐vypr |NordPHP中复合数据类型包括两种&#xff0c;即数组和对象。array(数组)&#xff1a;一组数据的集合。object(对象)&#xff1a;对象是类型的实例&#xff0c;使用new命令来创建。数组(array)数组是…

Python守护进程和脚本单例运行

2019独角兽企业重金招聘Python工程师标准>>> 一、简介 守护进程最重要的特性是后台运行&#xff1b;它必须与其运行前的环境隔离开来&#xff0c;这些环境包括未关闭的文件描述符、控制终端、会话和进程组、工作目录以及文件创建掩码等&#xff1b;它可以在系统启动…

分析access.log

cat access.log | awk {print $4,$1,$9} | awk -F/ {print $3}| awk -F: {print $2 ":" $3,$4} | awk {print $1,$3,$4} | uniq -c | sort -n转载于:https://www.cnblogs.com/olderblue/p/4778339.html

AD20学习笔记4---网表导入及模块化布局设计

前言&#xff1a; 本文学习视频是B站点击率第一的凡亿教育《Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设计视频教程》&#xff0c;视频地址&#xff1a;Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设…

Paoding-Rose学习

* HttpServletRequest.getContextPath 获取web程序root。如果是默认位置&#xff0c;返回””空串&#xff0c;否则返回 /根路径名 * rose是如何扫描到资源的 利用spring提供的类扫描类和jar* rose建立匹配树的过程 传入根节点和List&#xff0c;按照路径建立每个节点 * Module…

楪祈机器人_饥荒 Inori楪祈人物MOD V20161211

使用说明&#xff1a;1.解压缩2.复制所有文件到游戏目录mods3.启动游戏&#xff0c;点击mods(模组)加载MOD适用游戏版本&#xff1a;理论上支持所有版本的饥荒(普通&#xff0c;巨人&#xff0c;海难&#xff0c;联机版)MOD说明&#xff1a;饥荒 Inori楪祈人物MOD&#xff1b;由…

javascript 模块化

2019独角兽企业重金招聘Python工程师标准>>> 一直好奇像node.js,require.js的模块化是怎么做的&#xff0c;在看了《你不知道的javascript》后&#xff0c;对js的模块化有了一些简单的了解。这本书真的还不错。 书里讲述了js的模块化的原理 和 现代js实现模块化的简…

AD20学习笔记5---PCB设计规则设置及PCB手工布线

前言&#xff1a; 本文学习视频是B站点击率第一的凡亿教育《Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设计视频教程》&#xff0c;视频地址&#xff1a;Altium Designer 20 19&#xff08;入门到精通全38集&#xff09;四层板智能车PCB设…