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 ...
随机推荐
- DevExpress控件经验集合
关于GridControl的可以先看这里:http://blog.csdn.net/dong413876225/article/details/8313094 增加新行,我用了AddNewRow,但是 ...
- 字符串比较操作类StringComparer
StringComparer这个字符串比较操作类.它继承了四种接口IComparer, IEqualityComparer, IComparer<string>, IEqualityCom ...
- ORB-SLAM使用方法
preparation:按照官網步驟完成ORB的安裝. 1.修改Camera calibration參數:到~/ORB_SLAM/Data/Settings.yaml修改 2.開啟終端機 -&g ...
- B1954 The xor-longest Path
爆炸入口 给定一颗带权值的,节点数为n的树,求树上路径最大异或和. solution: 先dfs将所有点到根的异或和算出来.然后放进tire树中贪心. #include<cstdio> # ...
- machine learning学习笔记
看到Max Welling教授主页上有不少学习notes,收藏一下吧,其最近出版了一本书呢还,还没看过. http://www.ics.uci.edu/~welling/classnotes/clas ...
- TCP心跳的意义
摘自:https://blog.csdn.net/bjrxyz/article/details/71076442 TCP新手误区–心跳的意义 背景 最近面试了很多的学生,发现很多TCP的新手对于TCP ...
- java的八种基本数据类型
据说表格的方式一目了然 一. java数据类型的取值范围如下: 注意:long型后如果不加 L 则默认为int型,float型如果不加 F 则默认为double型: 注意!注意!注意 ...
- 交换机基础配置之结合以太通道的vlan设置
我们将以上面的拓扑图来做实验,建立以太通道,并设置好vlan,将pc1和pc3放在同一vlan,将pc2和pc4放在同一vlan,同一vlan能跨交换机通信 在一切还没布置之前,四台pc机都在同一网段 ...
- mysql 按值排序
order by FIELD(AuditorStatus,3,0,1,2) laravel 写法 $data = $query->orderByRaw(\DB::Raw('FIELD(Audit ...
- SQL Server 数据库内部版本号
数据库还原或版本升级出现版本错误时可参考 可以利用SQL语句查看本机数据库版本:select @@VERSION 如果你不确定是源数据库的版本,下面的映射表的SQL Server版本的内部版本号,以便 ...