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 ...
随机推荐
- linux - 文件查找及压缩
文件查找: 1. which 查找可以执行文件,只搜索$PATH里的目录 $ which ls /bin/ls which只搜索$PATH里的目录,如果搜索当前目录的文件是没有任何结果的 $ ...
- set_ip_pool
#! /usr/bin/env python# -*- coding: utf-8 -*- import redisimport urllib2class RedisConnect: #clas ...
- truncate与delete 、drop的区别
注意事项 1.在oracle中数据删除后还能回滚是因为它把原始数据放到了undo表空间. 2.DML语句使用undo表空间,DDL语句不使用undo, 而delete是DML语句,truncate是 ...
- 用bat使用date和time命令
D:\>date /T 2010-12-10 星期五 D:\>echo %date:~0,10% 2010-12-10 date:命令(别忘记date后面有个冒号) ~0:从索引0开始取内 ...
- python截取中文字符串
python的中文处理还是比较麻烦的,utf-8的字符串的长度是1-6个字符,一不小心就会从中截断,出现所谓的乱码.下面这个函数提供了,从一段utf-8编码的字符串中,截取固定长度的字串.ord(ch ...
- 【转】web常见安全问题以及测试方法
web安全是我们测试组一直以来作为和性能测试并驾齐驱的两个重点.开发的过程中还需要着重注意,该转义的地方转义:该屏蔽的地方屏蔽,该过滤的地方过滤等等.年底又到了,势必又有大批的发号抽奖之类的活动开发. ...
- Linux 命令 - free: 显示系统的内存信息
命令格式 free [-b | -k | -m] [-o] [-s delay ] [-t] [-l] [-V] 命令参数 -b 显示内存的单位为 Byte. -k 显示内存的单位为 KB. -m 显 ...
- IntelliJ IDEA 13.x 下使用Hibernate + Spring MVC + JBoss 7.1.1
从2004年开始做.NET到现在.直到最近要做一些JAVA的项目,如果说100个人写一篇关于.NET的文章,估计这10个人写的内容都是一样.但是如果说10个人写Java的文章,那真的是10个人10种写 ...
- Android实现双进程守护 (转)
做过android开发的人应该都知道应用会在系统资源匮乏的情况下被系统杀死!当后台的应用被系统回收之后,如何重新恢复它呢?网上对此问题有很多的讨论.这里先总结一下网上流传的各种解决方案,看看这些办法是 ...
- C盘空间不足,释放C盘空间
最近电脑总是特别卡,后来发现C盘空间严重不足,只剩下几十兆,以前最严重的时候是剩下0kb可以,怎一个惨字了得... 我所知道的C盘空间不足会导致的几个主要问题有: 1)拷贝大文件会失败.因为拷贝和剪切 ...