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 ...
随机推荐
- mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap【转】
由于mybatis简单易学,比起Hibername来,更容易上手,代码也能自动生成.这几天研究了下代码自动生成的,参考: http://0609xiaohua.iteye.com/blog/14535 ...
- jQuery扩展工具方法
共享学习Jquery源码的一些东西 jQuery.extend({ expando : 生成唯一JQ字符串(内部) noConflict() : 防止冲突 ---------------- ...
- 使用markdown及highlight
一.markdown 安装markdown2 pip install markdown2 应用markdown2 进入blog APP,创建templatetags文件夹,在文件夹内创建__init_ ...
- centos7重置root开机登录密码
今天忘记了centos7 root登录的密码,本来要好好的做个图文的教程也好啊,但是忘记截图什么的,就不在重复的工作了, 参考了下面的两个链接重置了密码,结合使用效果更好哦,嘿嘿.. 下次要是再遇到这 ...
- ss 如何解决margin-top使父元素margin失效
给子元素设置margin-top的时候父元素的也会受影响.会产生子元素和父元素margin合并的问题. 解决办法: 给父元素设置padding.或者border把父子的margin之间隔开. 一般来说 ...
- Java Concurrency - wait & notify, 等待通知机制
生产者消费者问题是一个常见的多线程同步案例:一组生产者线程和一组消费者线程共享一个初始状态为空.大小为 N 的缓冲区.只有当缓冲区没满的时候,生产者才能把消息放入缓冲区,否则必须等待:只有缓冲区不空的 ...
- C#的提交表单方式WebClient
向网站提交页面的主要代码 string postString = "arg1=a&arg2=b";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form ...
- Is it possible to change the iPhone device name programmatically?
今天刚好方案公司谈到一个需求方案,要制作一个dvr连接手机,手机能上网的功能. 为了简化,让dvr开机轮询,连接某个iphone设备名字特征的手机,希望在app中提供一个输入框,可以 按dvr可以识别 ...
- IOS中的内存不足警告处理(译)
由于在IOS中虚拟内存系统不会采用页置换的方式来获取请求内存,取而代之的是它通过移除应用程序中的强引用来释放一些内存资源,我们知道强引用在IOS中表示拥有关系,只要有至少一个变量拥有这个对象,那么对象 ...
- Php 操作事务
PHP来操作数据库 关于事务操作 连接数据 mysql_connect('localhost','root','123'); 设置字符集 mysql_query('set names utf8'); ...