the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043

and the source code is as followed.

#include<iostream>
#include<vector>
#include<cstdio> using namespace std;
int pos; struct cell
{
cell *lchild;
cell *rchild;
int c;
cell()
{
lchild = rchild = NULL;
}
}; void insert1(cell *&root,int x)
{
if (!root)
{
root = new cell;
root->c = x;
}
else
{
if (x < root->c)
{
insert1(root->lchild,x);
}
else
insert1(root->rchild,x);
}
}
void insert2(cell *&root,int x)
{
if (!root)
{
root = new cell;
root->c = x;
}
else
if (x > root->c)
{
insert2(root->lchild, x);
}
else
insert2(root->rchild, x);
}
void preOrder(cell *root,vector<int> &v)
{
v.push_back(root->c);
if (root->lchild != NULL)
preOrder(root->lchild,v);
if (root->rchild != NULL)
preOrder(root->rchild,v);
} void postOrder(cell *root,vector<int> &v)
{
if (root->lchild != NULL)
postOrder(root->lchild, v);
if (root->rchild != NULL)
postOrder(root->rchild, v);
v.push_back(root->c);
} int main()
{
int n;
vector<int> v1,v2,v3,v;
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
int x;
scanf("%d",&x);
v1.push_back(x);
}
cell *r = NULL;//THE root of bst
cell *r1 = NULL;//the root of mirror of bst;
for (int i = 0; i < n; i++)
{
insert1(r,v1[i]);
insert2(r1,v1[i]);
}
preOrder(r,v2);//the preorder of bst
preOrder(r1,v3);//the preorder of the mirror of bst if (v2 != v1 && v3 != v1)
{
printf("NO\n");
return 0;
}
else
{
printf("YES\n");
if (v2 == v1)
{
postOrder(r,v);
for (int i = 0; i < n; i++)
{
printf("%d%c",v[i], ((i - n + 1) ? ' ' : '\n'));//this type of presentation is fitted for the pattern of pat. }
}
else if(v3 == v1)
{
postOrder(r1,v);
for (int i = 0; i < n; i++)
printf("%d%c", v[i], ((i - n + 1) ? ' ' : '\n'));
}
}
return 0;
}

i think what is the most important for the bst-like problem is build a bst. that’s the point.

as long as the bst is built,then some trial things remains.

1043. Is It a Binary Search Tree (25)的更多相关文章

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

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

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

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

  5. PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)

    简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...

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

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

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

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

  8. pat1043. Is It a Binary Search Tree (25)

    1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

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

随机推荐

  1. 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

    // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...

  2. C语言——递归练习

    1.炮弹一样的球状物体,能够堆积成一个金字塔,在顶端有一个炮弹,它坐落在一个4个炮弹组成的层面上,而这4个炮弹又坐落在一个9个炮弹组成的层面上,以此类推.写一个递归函数CannonBall,这个函数把 ...

  3. Camera图片特效处理综述(Bitmap的Pixels处理、Canvas/paint的drawBitmap处理、旋转图片、裁截图片、播放幻灯片浏览图片<线程固定时间显示一张>)

    一种是直接对Bitmap的像素进行操作,如:叠加.边框.怀旧.(高斯)模糊.锐化(拉普拉斯变换). Bitmap.getPixels(srcPixels, 0, width, 0, 0, width, ...

  4. 题目1434:今年暑假不AC (项目安排类:结束时间快排,判断开始时间)

    题目描述: “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为 ...

  5. java 复习002

    java东西太多了,我都有点小凌乱了,记得太没结构了 java内存回收机制:垃圾收集GC(Garbage Collection) 两种常用方法: 引用计数(早期使用) 简介:堆中对象每次被栈中引用指向 ...

  6. hadoop多次格式化后,导致datanode启动不了,怎么办?(伪分布式)

    根据当初 hadoop 安装目录下 conf 目录的 core-site.xml 的设置,找到该目录:   进入该目录 在 data 和 name 文件夹下均有  current 文件夹 ,和 cur ...

  7. tomcat server获取用户的请求地址

    当用户 与 tomcat之间 用 nginx做跳转时, HttpServletRequest 中的 getRemoteHost()方法获取到的只是nginx的地址,而不能拿到用户真正的请求地址 解决方 ...

  8. 最长回文子串(Longest Palindromic Substring)-DP问题

    问题描述: 给定一个字符串S,找出它的最大的回文子串,你可以假设字符串的最大长度是1000,而且存在唯一的最长回文子串 . 思路分析: 动态规划的思路:dp[i][j] 表示的是 从i 到 j 的字串 ...

  9. Linux(Centos)全自动异地备份数据(WEB+Mysql)

    文章开始之前,先问下各位站长一个问题:什么东西对于站长是十分重要的?其实对于站长而言,很多东西都是很重要的.但我们现在排除外在因素,把范围缩小到网站系统本身,哪些是非常重要的呢?网站数据就是其中之一了 ...

  10. linux下登陆用户的行为信息—w和who命令详解

    查看用户的操作系统管理员若想知道某一时刻用户的行为,只需要输入命令w 即可,在SHELL终端中输入如下命令: [root@localhost ~]# w 可以看到执行w命令及显示结果. 命令信息含义上 ...