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 ...
随机推荐
- EF中 实现延迟加载 lazyload
1.创建数据库 2.利用数据库 生成视图 生成2个实体类 和一个model1类 3.写代码 (1) 创建 上下文对象 (2) (3)查询结果 注释: 延迟加载的原因,因为我们操作数据库不会那么简单, ...
- 【洛谷P3952】[NOIP2017]时间复杂度
时间复杂度 题目链接 对于 100%的数据:L≤100 . 很明显的模拟题 然而考试时还是爆炸了.. 调了一下午.. 蒟蒻表示不会离线操作.. 直接贴代码: #include<cstdio> ...
- 线程池,多线程,线程异步,同步和死锁,Lock接口
线程池 线程池,其实就是一个容纳多个线程的容器,其中的线程可以反复使用,省去了频繁创建线程对象的操作,无需反复创建线程而消耗过多资源. 除了创建和销毁线程的开销之外,活动的线程也需要消耗系统资源.线程 ...
- shell编程中的vim命令说明
vim命令模式: 1.一般命令模式 2.编辑模式 3.底行命令行命令模式 一般命令模式 直接用字符操作编辑模式 可以写文档(跟txt有点像)底行命令模式 先按'ESC',在按下“:”,之后在输出命令 ...
- C#中datatable的用法/传数据
在开发中,我们常用到表类型数据,不同于string,int....那么datatable类型如何定义呢,具体怎么使用呢,代码如下: namespace Common.Table { using Sys ...
- MySQL事务隔离级别 解决并发问题
MySQL事务隔离级别 1. 脏读: 骗钱的手段, 两个窗口或线程分别调用数据库转账表,转账后未提交,对方查看到账后,rollback,实际钱没转. 演示方法: mysql默认的事务隔离级别为repe ...
- CSS3一些特殊属性
(一)-webkit-tap-highlight-color 这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...
- LeetCode47.Permutations II(剑指offer38-1)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 私有maven库发布及使用流程
## 私有maven库发布流程 ### 环境配置 - idea环境下,如果使用内置maven,需要手动生成settings.xml,并关联. - 操作如下 - 生成settings.xml 右键pom ...
- 前端之jquery函数库
jquery介绍 jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. ...