http://www.patest.cn/contests/pat-a-practise/1043

 #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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Debian 7 安装 Docker

    Debian 7更新内核到3.16后 一.添加docker源 在source.list中加入: # Docker Repo deb https://get.docker.io/ubuntu docke ...

  2. C#中登录验证FormsAuthentication

    1:前台编写一个登录界面,这里为了简化,只有用户名和密码 代码如下: <form method="post" action="/User/CheckLogin&qu ...

  3. 【安卓面试题】使用SQLiteOpenHelper的getReadableDatabase()获得的数据库能不能,做写的操作

    可以! 不要被Readable的意思误导啦,readable是可读的意思,但不代表不能写哦. getReadableDatabase() 会获取用于操作SQLiteDatabase的实例. getRe ...

  4. mysql 修改 character_set_database 编码格式

    操作系统:win10  x64 Server version : 5.5.46 MySQL Community Server (GPL) mysql 修改 character_set_database ...

  5. poj 2987 最大闭合子图

    思路: 这题考的是最大闭权图.只要知道怎么求最大闭权图就知道怎么做.但好像有点卡模版,要高效的模版才行. #include <iostream> #include <stdio.h& ...

  6. 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常的解决办法

    为了避免这种异常,我们可以使用CopyOnWriteArrayList来代替ArrayList,CopyOnWriteArrayList支持并发访问,所以同时进行迭代和修改是没有问题的.

  7. iOS下编译ffmpeg

    网络上搜索“ios ffmpeg 编译”,文章一大把,但我编译还是费了很大的功夫才编译成功.很多文章只是把步骤列了出来,但是每个人的系统环境,或者程序版本都不一样,结果出现各种的错误.我把自己编译过程 ...

  8. Jquery Easyui验证扩展,Easyui验证,Easyui校验,js正则表达式

    Jquery Easyui验证扩展,Easyui验证,Easyui校验,js正则表达式 >>>>>>>>>>>>>> ...

  9. 微信小程序(原名微信应用号)开发工具0.9版安装教程

    微信小程序全称微信公众平台·小程序,原名微信公众平台·应用号(简称微信应用号) 声明 微信小程序开发工具类似于一个轻量级的IDE集成开发环境,目前仅开放给了少部分受微信官方邀请的人士(据说仅200个名 ...

  10. asp.net常见面试题(一)

    1.索引器 class Player { ]; public int this[int index] { get { || index >= ) { ; } else { return arr[ ...