简单题。构造出二叉搜索树,然后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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)

    题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 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 ...

随机推荐

  1. Network view

    network view 组件用来在局域网之内去同步一个游戏物体的组件属性,只响应创建它的那个客户端事件

  2. Linux系统采用netstat命令查看DDOS攻击的方法

    Linux系统采用netstat命令查看DDOS攻击的方法 来源:互联网 作者:佚名 时间:07-05 15:10:21 [大 中 小] 这篇文章主要为大家介绍了Linux系统采用netstat命令查 ...

  3. python unitest基本

    基本 import unittest class OneTest(unittest.TestCase): def setUp(self): self.verificationErrors = [] s ...

  4. int与byte的区别

    Java中涉及byte.short和char类型的运算操作首先会把这些值转换为int类型,然后对int类型值进行运算,最后得到int类型的结果.因此,如果把两个byte类型值相加,最后会得到一个int ...

  5. hdu_3966_Aragorn's Story(树链剖分裸题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...

  6. Python实战:爬虫的基础

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...

  7. 购物车(Shopping cart) —— B2C网站核心产品设计 (二)

    购物车是做什么的? 我们先来看一下现实超市中的购物车,一个带四个轱辘的铁筐子,客人推来推去,看到什么东西喜欢,就扔进去,觉得东西差不多了,就推到收银台. 那B2C网站中的购物车又是一个什么东西呢? 从 ...

  8. asp.net如何删除文件夹及文件内容操作

    static void DeleteDirectory(string dir) { && Directory.GetFiles(dir).Length == ) { Directory ...

  9. WPF中ListBox控件选择多个数据项

    XAML: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft ...

  10. Echarts自适应浏览器大小

    var myChart = echarts.init(document.getElementById('sitesChar')); var option = { title : { text: 'No ...