PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题。构造出二叉搜索树,然后check一下。
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn=+;
struct Node
{
int left;
int right;
int val;
} s[maxn]; int n;
int a[maxn];
int ans[maxn],tot;
int h[maxn];
int k; void dfs(int x)
{
ans[k++]=s[x].val;
if(s[x].left!=-) dfs(s[x].left);
if(s[x].right!=-) dfs(s[x].right);
} void houxu(int x)
{
if(s[x].left!=-) houxu(s[x].left);
if(s[x].right!=-) houxu(s[x].right);
h[k++]=s[x].val;
} bool check()
{
for(int i=;i<n;i++) if(ans[i]!=a[i]) return ;
return ;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=; i<n; i++) scanf("%d",&a[i]);
for(int i=; i<=n; i++) s[i].left=s[i].right=-;
int id=;
s[id++].val=a[];
for(int i=; i<n; i++)
{
int now=;
while()
{
if(a[i]<s[now].val)
{
if(s[now].left!=-) now=s[now].left;
else
{
s[now].left=id;
s[id++].val=a[i];
break;
}
}
else
{
if(s[now].right!=-) now=s[now].right;
else
{
s[now].right=id;
s[id++].val=a[i];
break;
}
}
}
} // for(int i=0;i<id;i++) printf("%d %d %d\n",s[i].val,s[i].left,s[i].right); k=;
dfs();
if(check()==)
{
printf("YES\n");
k=; houxu();
for(int i=;i<n;i++)
{
printf("%d",h[i]);
if(i<n-) printf(" ");
else printf("\n");
}
continue;
} k=;
for(int i=;i<n;i++) swap(s[i].left,s[i].right);
dfs();
if(check()==)
{
printf("YES\n");
k=; houxu();
for(int i=;i<n;i++)
{
printf("%d",h[i]);
if(i<n-) printf(" ");
else printf("\n");
}
continue;
} printf("NO\n"); }
return ;
}
PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)的更多相关文章
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- 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 ...
- 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分)(树的插入)
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)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 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 ...
随机推荐
- 取出parentid为null的顶级栏目 等号改为 is null 避免null当做字符串,
mysql中查询字段为null或者不为null 在mysql中,查询某字段为空时,不可用等号 = null, 而是 is null,不为空则是 is not null select * from ...
- WCF:调用方未由服务器进行身份验证
错误描述: 1. WCF:调用方未由服务器进行身份验证 2. 无法处理消息.这很可能是因为操作“http://tempuri.org/ISCCLSvc/GetCarriersByWareHouse”不 ...
- 数据库sql语句为什么要用绑定形式?
基于两点: 1,安全性,防sql注入: 2,共享资源,相似的sql能被缓存而不是重新解析. 淘测试给出了一个很好的理由:http://www.taobaotesting.com/blogs/859
- Google 分布式关系型数据库 F1
F1是Google开发的分布式关系型数据库,主要服务于Google的广告系统.Google的广告系统以前使用MySQL,广告系统的用户经常需要使用复杂的query和join操作,这就需要设计shard ...
- ecshop foreach循环判断循环次数
首先要在foreach里面加上一个name属相,如:name=name如:<!-- {foreach from=$package_goods.goods_list item=goods_list ...
- 使用Chrome DevTools的Timeline和Profiles提高Web应用程序的性能
来源: http://www.oschina.net/translate/performance-optimisation-with-timeline-profiles 我们都希望创建高性能的Web应 ...
- CentOS6.6 搭建Zabbix_3.0
公司有下发内网监控服务器的需求 使用zabbix监控 所以这篇文章是讲述的zabbix的搭建 其实网上很多地方都有 可以参考 环境安装 系统环境: # cat /etc/redhat-release ...
- RS232 DB9 公头 母头 串口引脚定义
转自:http://hi.baidu.com/zhy_myspace/item/ad1ab0f112a4da1ad7ff8cf9 DB9 公头 母头 串口引脚定义 1.RS-232端(DB9母头/孔型 ...
- auto_ptr浅析
auto_ptr是C++标准库中(<utility>)为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针) auto_ptr的实现原理其实就是RAII,在构造的 ...
- linux 命令--sed
简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中的内容,处 ...