1043. Is It a Binary Search Tree
#include <stdio.h>
#include <vector>
using namespace std; struct Node
{
Node* lchild;
Node* rchild;
int val;
}Tree[]; int loc,loc2,loc3;
Node* create()
{
Tree[loc].lchild=NULL;
Tree[loc].rchild=NULL;
return &Tree[loc++];
} Node* insert1(Node* T,int v)//二叉排序树构建
{
if(T==NULL)
{
T=create();
T->val=v;
}
else
{
if(v<T->val) T->lchild=insert1(T->lchild,v);
else T->rchild=insert1(T->rchild,v);
}
return T;
} Node* insert2(Node* T,int v)//镜像二叉排序树构建
{
if(T==NULL)
{
T=create();
T->val=v;
}
else
{
if(v>=T->val) T->lchild=insert2(T->lchild,v);
else T->rchild=insert2(T->rchild,v);
} return T;
} void preorder(Node *T,int ans[])
{ if(T!=NULL)
{
ans[loc2++]=T->val;
if(T->lchild!=NULL) preorder(T->lchild,ans);
if(T->rchild!=NULL) preorder(T->rchild,ans);
}
} void postorder(Node* T,int ans[])
{
if(T!=NULL)
{
if(T->lchild!=NULL) postorder(T->lchild,ans);
if(T->rchild!=NULL) postorder(T->rchild,ans);
ans[loc3++]=T->val;
}
} bool compare(int a[],int b[],int n)//比较序列
{
bool bo=true;
for(int i=;i<n;i++)
{
if(a[i]!=b[i])
{
bo=false;break;
}
} return bo;
} int main()
{
int n,i;
//二叉排序树的前序就是构建树的插入顺序
//所以只要按照前序建立二叉排序树 和 镜像二叉排序树,得出两者的前序
//与输入的序列比较,如果其中之一相等,则YES,并输出该树的中序。否则,NO。
while(scanf("%d",&n)!=EOF)
{
getchar();
int v1[];//存储 输入序列
int pre1[];//存储 二叉排序树的前序
int pre2[];//存储 镜像二叉排序树的前序
int post[];//存储 后序遍历
loc=; loc3=;
Node *T1=NULL,*T2=NULL;
for(i=;i<n;i++)//建树
{
scanf("%d",&v1[i]);
T1=insert1(T1,v1[i]);
T2=insert2(T2,v1[i]);
}
getchar();
loc2=;
preorder(T1,pre1);
loc2=;
preorder(T2,pre2);
bool b1=compare(v1,pre1,n),b2=compare(v1,pre2,n); if(!b1&&!b2)
{
printf("NO\n");
}
else
{
printf("YES\n");
if(b1) postorder(T1,post);
else postorder(T2,post); for(i=;i<n;i++)
printf("%d%c",post[i],i==n-?'\n':' ');
}
}
return ;
}
1043. Is It a Binary Search Tree的更多相关文章
- 【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 ...
- PAT 1043 Is It a Binary Search Tree[二叉树][难]
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- 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 ...
- 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 ...
随机推荐
- CoreDate的使用
勾选 xcode的 CoreDate会帮我们自动创建 CoreData 但是我们通常不那样使用,通常把 CoreDate 在单利类中创建, // // ZYDAO.h // StoryboardTes ...
- Android调用相册截取图片遇到的问题
1.在Android中可以使用如下的方式来调用相册,选择图片进行裁剪使用,昨天在开发的时候遇到一个问题 private void cropBigImageUri(Uri uri, int output ...
- [转]Oracle hang分析
hanganalyze是ORACLE的一款性能诊断工具,这个款工具是从oracle 8.0.6开始可用,在oracle数据库出现严重的性能问题的时候它可以帮助你定位问题所在. 1.首先说说hangan ...
- TCP基础知识
TCP/IP网络协议栈分为应用层(Application).传输层(Transport).网络层(Network)和链路层(Link)四层.如下图所示 两台计算机通过TCP/IP协议通讯的过程如下所示 ...
- ActiveMQ(5.10.0) - Configuring the Simple Authentication Plug-in
The easiest way to secure the broker is through the use of authentication credentials placed directl ...
- AjaxForm
近乎的Ajax控件介绍,代码下载:http://www.jinhusns.com/Products/Download?type=whp AjaxForm 概述 功能说明 基于 ajaxForm 插件进 ...
- VSFTPD无法上传的解决方法
搭建好FTP之后就没有去测试了,今天去试了一下上传的时候发生错误了,无法上传,提示“553 Could not create file”错误, 上网找了一些资料,发现很多都说是权限和防火墙的问题,但是 ...
- 关于微信response_type参数错误
可能出现问题的几个原因: 1.正确的2.0auth的地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=1%&redire ...
- MVC3、如何应用EntityFramework 连接MySql 数据库
原文:MVC3.如何应用EntityFramework 连接MySql 数据库 新的一年,新的开始. 今天总结的主题是在MySql中应用EntityFramework 的Code First模式. 开 ...
- Oracle 硬解析查询
-- 硬解析的 parse count (hard) select * from v$sysstat where name like '%parse%'; select a.value,b.name ...