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 ...
随机推荐
- 1045 | error connecting to master 'slave_user@192.168.0.75:3306' - retry-time: 6
mysql 主从复制问题整理 问题: 1045 | error connecting to master 'slave_user@192.168.0.75:3306' - retry-t ...
- set_ip_pool
#! /usr/bin/env python# -*- coding: utf-8 -*- import redisimport urllib2class RedisConnect: #clas ...
- Linux中ifcfg-eth0配置参数解释
Linux中设置IP地址经常使用到ifcfg-eth0这个文件. vi /etc/sysconfig/network-scripts/ifcfg-eth0 附录文件中的内容: DEVICE=eth0 ...
- 命名管道FIFO
首先我得检讨一下自己,这几天有些颓呀,打不起精神,板子出了点问题,果真自学还是很困难呀,硬件方面难解决呀,理想与现实还是很有差距的,伤透了,凌乱了. 一直在理解进程间通信的问题.发现上次忽略了一个问题 ...
- HDU 1003 - Max Sum(难度:*)
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- 为什么Java byte 类型的取值范围是-128~127 (转)
概念:负数的补码是该 数 绝 对 值 的 原 码 按 位 取 反 ,然 后 对 整个数 加 1 步骤: 1.确定byte是1个字节,也就是8位 2.最大的应该是0111 1111,因为第一位是符号位, ...
- P2184 贪婪大陆
P2184 贪婪大陆 题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前 ...
- CSS—换行
关于文本溢出换行问题,先看下涉及到换行的相关属性,查看:http://www.w3school.com.cn 一.word-break 定义:word-break 属性规定自动换行的处理方法. 值 描 ...
- Android经验之谈1
豌豆荚等可能会导致手机网络出现问题,电话打不进来,所以需要及时卸载豌豆荚. 系统apk,不能通过在manifest.xml里面添加origin-package或者useid等来改变. 而是需要用and ...
- Apache httpd开启SSL
https://my.oschina.net/xpbug/blog/197454 必要软件 apache httpd 2.4(windows) perl 5 (windows) openssl (wi ...