1043 Is It a Binary Search Tree (25 分)(二叉查找树)

#include<bits/stdc++.h> using namespace std;
typedef struct node;
typedef node *tree;
struct node
{
int data;
tree L,R;
};
void Insert(tree &bt,int x)
{
if(bt==NULL){
bt=new node;
bt->data=x;
bt->L=NULL;
bt->R=NULL;
return;
}
if(x<bt->data) Insert(bt->L,x);
else Insert(bt->R,x);
}
vector<int>pre1,pre2,post1,post2;
void preorder1(tree bt,vector<int>&p)
{
if(bt){
p.push_back(bt->data);
preorder1(bt->L,p);
preorder1(bt->R,p);
}
}
void preorder2(tree bt,vector<int>&p)
{
if(bt){
p.push_back(bt->data);
preorder2(bt->R,p);
preorder2(bt->L,p); }
} void postorder1(tree bt,vector<int>&p)
{
if(bt){
postorder1(bt->L,p);
postorder1(bt->R,p);
p.push_back(bt->data);
}
} void postorder2(tree bt,vector<int>&p)
{
if(bt){
postorder2(bt->R,p);
postorder2(bt->L,p);
p.push_back(bt->data);
}
}
int main()
{
int n;
scanf("%d",&n);
tree bt;
bt=NULL;
vector<int>tt;
for(int i=;i<n;i++){
int x;
scanf("%d",&x);
tt.push_back(x);
Insert(bt,x);
}
preorder1(bt,pre1);
preorder2(bt,pre2);
postorder1(bt,post1);
postorder2(bt,post2);
if(tt==pre1){
puts("YES");
for(int i=;i<post1.size();i++){
if(i) printf(" ");
printf("%d",post1[i]);
}
}
else if(tt==pre2){
puts("YES");
for(int i=;i<post2.size();i++){
if(i) printf(" ");
printf("%d",post2[i]);
}
}
else{
puts("NO");
}
return ;
}
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 ...
- 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甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- 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)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- 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特别不喜欢那些随便转载别人的原创文章又不给 ...
- 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 ...
随机推荐
- 判断团队适不适合使用node
1.要不要用 2.历史包袱 3.跟进升级 看完scott创业公司使用node,对于一个团队要不要使用node,第一个就是如果承接的项目有很多历史迭代,线上也在稳定的抛,不要轻易的替换,比如很多老代码, ...
- scrapy install
csf@ubuntu:~$ sudo apt install python-scrapy
- js最佳实践
JavaScript使用windows对象的open()方法来创建新的浏览器窗口,这个方法有三个参数:windows.open(url,name,features) 参数一:url:是想在新窗口里打开 ...
- 【学时总结】◆学时·V◆ 逆元法
◆学时·V◆ 逆元法 □算法概述□ 逆元运算是模运算中的一个技巧,一般用于解决模运算的除法问题.模运算对于加.减.乘是有封闭性的,即 (a±b)%m=a%m±b%m,以及 (a×b)%m=a%m×b% ...
- spring-bean(全生命周期)
作用:在初始化和销毁bean时候,做一些处理工作是调用生命周期方法 格式: <bean id=”该生命周期的名称” class=”提供方法的类的全路径” init-methood=”init” ...
- MySql开启GTID和多线程复制功能
1.修改参数 master: gtid_mode = ON --开启gtid这个必须打开 enforce-gtid-consistency = ON ...
- 精读《12 个评估 JS 库你需要关心的事》
1 引言 作者给出了从 12 个角度全面分析 JS 库的可用性,分别是: 特性. 稳定性. 性能. 包生态. 社区. 学习曲线. 文档. 工具. 发展历史. 团队. 兼容性. 趋势. 下面总结一下作者 ...
- 【shopex】真正可用的app开发机制
shopex的app开发机制详解 网上流传的shopex4.8.5的app开发教程,不仅说得不明不白,而且由于版本问题,照着做根本是做不成的. 知其然,亦要知其所以然. shopex提供了的一个干 ...
- MAC中向阿里云服务器上传文件
打开mac中的终端 使用命令:$scp /local/file user@remote:/file /local/file 是本地文件 后面部分[用户名]@[ip地址:][服务器中的文件目录] not ...
- 课时16.HTML-XHTML-HTML5区别(了解)
简而言之 HTML语法非常宽松容错性强: XHTML更为严格,它要求标签必须小写,必须严格闭合,标签中的属性必须使用引号引起等等. HTML5是HTML的下一个版本所以除了非常宽松容错性强以外,还增加 ...