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 ...
 
随机推荐
- 下拉的DIV+CSS+JS二级树型菜单
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - 昨天上架出现问题,you binary is not optimized for iphone5.。。。。
			
这个时候只需要加一个lanuch image 就可以了
 - 开始制作国产的 scratch
			
首先分析下API和数据格式: https://api.scratch.mit.edu/proxy/featured GET 23.235.37.162:443 返回数据与对应栏目: commun ...
 - Service的启动方式
			
Service的启动方式: 两种启动模式,一种是非绑定启动模式,另一种是绑定启动模式. 一.startservice方式启动 Intent intent = new Intent(this, Firs ...
 - vs调试有时能进去后台,有时不能进去
			
前两天做项目时,遇到调试时有时候能进后台,有时候直接就弹出运行结果,反复查找原因,最后发现,原来是页面输出缓存的原因,我在web页面用到了< OutputCache Duration=" ...
 - Linux系统的信号详解
			
一.信号类型 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) ...
 - perl-cgi-form
			
来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698595.html http://www.cnblogs.com/itech/archiv ...
 - iOS \'The sandbox is not sync with the Podfile.lock\'问题解决
			
iOS \'The sandbox is not sync with the Podfile.lock\'问题解决 HUANGDI 发表于 2015-02-27 09:51:13 问题描述: gith ...
 - java的两种异常runtimeException和checkedException
			
java异常处理机制主要依赖于try,catch,finally,throw,throws五个关键字. try 关键字后紧跟一个花括号括起来的代码块,简称try块.同理:下面的也被称为相应的块. ...
 - 对比React Native、dcloud、LuaView三个框架技术(内部)
			
转载自:http://www.jianshu.com/p/ee1cdb33db8d主要对比React Native和5+SDK(就是dcloud的SDK)两个: 开发语言:三个都是用其他语言来统一开发 ...