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 ...
随机推荐
- Windows server 2008搭建php运行环境
下载php组件包 首先到http://windows.php.net/download/下载你需要的php版本,这里我下载的是php5.3. 下面解压php组件 包到磁盘上. 安装Microsoft ...
- classname 就是在css上添加类,然后js的类名等于
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...
- CCF考试真题题解
CCF考试认证:题解参考博客http://blog.csdn.net/u014578266/article/details/45221841 问题描述 试题编号: - 试题名称: 图像旋转 时间限制: ...
- Number-guessing Game
Number-guessing Game Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Othe ...
- android 瀑布流效果(仿蘑菇街)
我们还是来看一款示例:(蘑菇街) 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此.就如我们的方角图形,斯通见惯后也就出 ...
- 几种访问其他域swf文件,或本地浏览器运行环境【安全沙箱】冲突解决方法
声明: 几种方法都源自网络,我只负责汇总一下子,因为来自多方转载,也找不到初始出处了,需要的可以随意收藏,物尽其用,蛮好^_^ 对于类似的出错信息:SecurityError: Error #2148 ...
- UIImageView的UserInteractionEnabled什么时候为no
UIImageView作为背景,但直接把按钮或者UITextField放在上面无法相应事件 特殊子类的覆盖 userInteractionEnabled属性默认值为YES,但UIView的一些子类中对 ...
- Myclipse 安装 Maven遇见的N个异常
1.Maven 下载好,配置完环境变量,同时在Myeclipse配置好Maven,这时创建Maven项目失败,报如下异常: Could not resolve archetype org.apache ...
- URL设置问题
URL设置那里删除了<item path="index.aspx" pattern="index.aspx"/>后,访问首页就不出来了,要加上/in ...
- UITabBarItem's appearance
1.我们知道,用tabBarController创建出来默认的tabBar似这个样子滴... -----------------我是图片分割线----------------------------- ...