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 ...
随机推荐
- CentOS系统中使用iptables设置端口转发
echo 1 > /proc/sys/net/ipv4/ip_forward 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0 ...
- android build.prop详解
# begin build properties开始设置系统性能 # autogenerated by buildinfo.sh{通过设置形成系统信息} ro.build.id=MIUI(版本ID) ...
- 第23章 I2C—读写EEPROM—零死角玩转STM32-F429系列
第23章 I2C—读写EEPROM 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- 统计文件中的URL
1. 题目: 一个文本文件中每一行中有一个URL,最多一万行,统计每一个URL的次数,输出到另外一个文件中,每一行前面是URL,后面是个数. 2.代码: package test; import ja ...
- EAIntroView–高度可定制的iOS应用欢迎页通用解决方案
简介 高度可定制的应用欢迎页通用解决方案,可高度定制,不要仅限于现有的demo. 项目主页: EAIntroView 最新示例: 点击下载 入门 安装 安装后,引入” EAIntroView.h”并设 ...
- mariadb源码编译安装及多实例
准备文件源文件/app/mariadb-10.2.12.tar.gz cd /app/ tar xf mariadb-10.2.12.tar.gz cd mariadb-10.2.12 mkdir ...
- JS - 简单的下载图片至本地
<iframe id="saveImg" src="图片路径" style="display:none;"></ifram ...
- Java高并发之线程基本操作
结合上一篇同步异步,这篇理解线程操作. 1.新建线程.不止thread和runnable,Callable和Future了解一下 package com.thread; import java.tex ...
- 【Python 2 到 3 系列】 print 是函数
v3.0 以前,print一直作为语法结构存在,他是python语法的一部分:这个理解起来可能有点蹩脚,但的确是这样. print 一直被定以为一个statement,也就是说,他跟return/tr ...
- 项目配置中 提示access denied的问题 解决方案
项目配置中 提示access denied的问题,一般原因是你的服务器或虚拟主机的pathinfo没开.... 具体解决办法如下 在PHP安装文件夹下找到php.ini.在文件中搜索cgi.fix_p ...