A1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.
Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line YES
if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or NO
if not. Then if the answer is YES
, print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO
#include<cstdio>
struct node{
int data;
node *lchild;
node *rchild;
};
node *newNode(int x){//生成新的节点
node *rt=new node;
rt->data=x;
rt->lchild=rt->rchild=NULL;
return rt;
}
void insert(node* &rt,int x){
if(rt==NULL){//在BST树中插入节点
rt=newNode(x);
return;
}
if(rt->data>x){
insert(rt->lchild,x);
}else{
insert(rt->rchild,x);
}
}
node *create(int data[],int n){//创建BST树
node *rt=NULL;
for(int i=;i<n;i++){
insert(rt,data[i]);
}
return rt;
}
node *swapBst(node *rt){//利用前序递归遍历,实现将所有节点的左子树
if(rt==NULL){//和右子树相互交换
return NULL;
}else{
node *temp=rt->lchild;
rt->lchild=rt->rchild;
rt->rchild=temp;
swapBst(rt->lchild);
swapBst(rt->rchild);
}
return rt;
}
int* preOrder(node *rt,int pre[],int &k){
if(rt==NULL){//将BST树遍历输出到指定数组中
return NULL;
}else{
pre[k++]=rt->data;
preOrder(rt->lchild,pre,k);
preOrder(rt->rchild,pre,k);
}
return pre;
}
bool isP(int data[],int temp[],int n){
for(int i=;i<n;i++){//判定两个数组中元素是否对应相等
if(data[i]!=temp[i]){
return false;
}
}
return true;
}
void postOrder(node *rt,int &n){//后序遍历输出二叉树
if(rt==NULL){
return;
}else{
postOrder(rt->lchild,n);
postOrder(rt->rchild,n);
if(n!=){
printf("%d ",rt->data);
n--;
}else{
printf("%d",rt->data);
}
}
}
int main()
{
int n;
int data[];
int pre[];
int mir[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&data[i]);
}
node *rt=create(data,n);
int k=;
preOrder(rt,pre,k);
if(isP(data,pre,n)){
printf("YES\n");
postOrder(rt,n);
return ;
}
node *mirr=swapBst(rt);
k=;
preOrder(mirr,mir,k);
if(isP(data,mir,n)){
printf("YES\n");
postOrder(rt,n);
return ;
}
printf("NO\n");
return ;
}
Mist Note:本题考察二叉搜索树(BST)的相关算法,首先要学会利用给定序列建立BST,建立之后会递归遍历,
灵活利用递归遍历交换所有节点的左右子树。
A1043 Is It a Binary Search Tree (25 分)的更多相关文章
- 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 (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- 04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
随机推荐
- ubuntu apache2配置多站点
ubuntu下使用sudo apt-get install apache2方法安装时,配置文件主要在/etc/apache2/目录下.主要有: apache2.conf : 主配置文件,会通过incl ...
- 把sublime3打造成c++开发环境
安装sublime 3 sudo add-apt-repository ppa:webupd8team/sublime-text-3 sudo apt-get update sudo apt-get ...
- Sort HDU - 5884 哈夫曼权值O(n)
http://acm.hdu.edu.cn/showproblem.php?pid=5884 原来求一次哈夫曼可以有O(n)的做法. 具体是,用两个队列,一个保存原数组,一个保存k个节点合并的数值,然 ...
- Spring RestTemplate GET 请求参数
@Test public void testUpdateProfitJson_GET_Params() throws BusinessException { String apiURL="U ...
- formatter 操作列表的合并
{field:'22',title:'操作',width:250,align:'center',sortable:true,formatter : function(value, row, index ...
- 大数据kafka视频教程 学习记录【B站尚硅谷 】
视频地址: https://www.bilibili.com/video/av35354301/?p=1 2019/03/06 21:59 消息队列的内部实现: Kafka基础: ...
- JavaScript初始
一.JavaScript基础 1.引入方式 <!--1 直接编写--> <script> alert('hello yuan') </script> <!-- ...
- linux下安装redis和部署
转自简书:https://www.jianshu.com/p/bc84b2b71c1c 1.基础知识 redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值 ...
- window.open()方法详解
, 最基本的弹出窗口代码 window.open('page.html'); 2, 经过设置后的弹出窗口 window.open('page.html', 'newwindow', 'heig ...
- nvcc 编译显示寄存器使用情况
NVCC Compiler 里面增加 Command line pattern中${COMMAND}后 增加选项: --ptxas-options=-v