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 ...
随机推荐
- Asp.net中,点击GridView表头实现数据的排序
一.实现该功能的基本工作. 1. 先添加一个GridView,取名为gvData. 2. 设置该控件的属性: 操作步骤如下 设置属性: 这4个属性,还要设置该控件AllowSorting=&quo ...
- 命令 shell 学习
for i in a b c do echo $i done !ser 历史补全 > 正确信息输出文件 >>正确信息输出文件 ,追加 2>错误信息输出文件 2>> ...
- JSP注释及scriptlet <%局部%><%!全局%><%=输出%>
显示注释: <!--注释内容-->> 隐式注释: 1. // 2./* */ 3. <%-- 注释内容--%> <!-- 这个注释客户端就可以看见 --> & ...
- 一篇很全面的freemarker教程 前端工程师必备
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...
- 常用 SQL语句
Oracle 查看所有表的名字: select table_name from user_tables; 查询特定表的名字:select * from (select table_name t fro ...
- 关于Winform中的用户代理
问题描述: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 解释: 1. 应用程序版本“Mozilla ...
- MySql-时间格式转换之转换为时分秒格式的日期
select date_format(create_datetime,'%Y-%m-%d %k:%i:%s') from busi_repairitem_category MySQL毫秒值和日期的指定 ...
- oracle sqlplus @@用法
本文章已收录于: .embody { padding: 10px 10px 10px; margin: 0 -20px; border-bottom: solid 1px #ededed } .e ...
- 1.1 python 安装(Windows)
1.浏览器输入URL http://www.python.org/download/ https://www.python.org/downloads/release/python-2712/ Win ...
- 在idea的maven项目使用el或jstl表达式
必须加上这句: <%@ page isELIgnored="false" %> 否则无法解析el或jstl表达式 <%@ taglib prefix=" ...