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 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)的更多相关文章
- 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 ...
- 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分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 【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 ...
随机推荐
- Java连接Oracle10g
1.导入驱动包: a.找到oracle安装目录下的jdbc/lib中的文件classes12.jar: b.右击你创建的JAVA工程,找到Build path,选择Add External Archi ...
- PHP 实现下载文件的方法
方法一: header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); ...
- 【软件多国语言】一个demo
之前上学的时候做过一个东西,需要中英文软件界面,并且需要随时可以切换,当时是师妹来做的,用的最直接也是最笨的办法, what? if(中文) { button1.Text = "花姑娘&qu ...
- Leave Morningstar
Today, closed 4++ years developer life (2009.11.17-2014.02.28) in MorningStar Shenzhen Office Austra ...
- JVM基础知识(1)-JVM内存区域与内存溢出
JVM基础知识(1)-JVM内存区域与内存溢出 0. 目录 什么是JVM 运行时数据区域 HotSpot虚拟机对象探秘 OutOfMemoryError异常 1. 什么是JVM 1.1. 什么是JVM ...
- LCD1602汉字、自定义字符取模
用zimo221软件, 新建一个8*8的图像,留出左边3列,用右边5列点出自定义字符,选择取模方式C51,就可得到对应的编码 如下图:温度符号℃的编码
- I - Control - HDU 4289 (最大流)
题意:有N个城市,现在城市S出现了一伙歹徒,他们想运送一些炸弹到D城市,不过警方已经得到了线报知道他们的事情,不过警察不知道他们所在的具体位置,所以只能采取封锁城市的办法来阻断暴徒,不过封锁城市是需要 ...
- display:none;与visibility:hidden;的区别
visibility:隐藏对应的元素但不挤占该元素原来的空间.display:隐藏对应的元素并且挤占该元素原来的空间. 下面来看visibility和dispaly的一些参数 visibility用来 ...
- dll开发中遇到的问题
刚碰到个问题,我的一个项目中引用了一个dll,这个dll又引用了另一个dll,我把这俩个都放在bin文件夹下,但是会报错,说第二个dll找不到.把它放到系统文件夹system32下就没事了. 但是遇到 ...
- Spring Data JPA教程, 第五部分: Querydsl(未翻译)
The fourth part of my Spring Data JPA tutorialdescribed how you can implement more advanced queries ...