1043 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 Ninteger 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
分析: 水题。。基本的二叉树前后遍历,镜像的意思就是遍历的时候交换左右子树即可
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-21-17.14.43
* Description : A1043
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int maxn=1010;
struct node{
int data;
node* lchild;
node* rchild;
}no[maxn];
vector<int> origin, pre, preM, post, postM;
void insert(node* &root,int data){
if(root==NULL){
root=new node;
root->data=data;
root->lchild=root->rchild=NULL;
return;
}
if(data<root->data) insert(root->lchild,data);
else insert(root->rchild,data);
}
void preOrder(node* root,vector<int>& vi){
if(root==NULL) return;
vi.push_back(root->data);
preOrder(root->lchild,vi);
preOrder(root->rchild,vi);
}
void preMirrorOrder(node* root,vector<int>& vi){
if(root==NULL) return;
vi.push_back(root->data);
preMirrorOrder(root->rchild,vi);
preMirrorOrder(root->lchild,vi);
}
void postOrder(node* root,vector<int>& vi){
if(root==NULL) return;
postOrder(root->lchild,vi);
postOrder(root->rchild,vi);
vi.push_back(root->data);
}
void postMirrorOrder(node* root,vector<int>& vi){
if(root==NULL) return;
postMirrorOrder(root->rchild,vi);
postMirrorOrder(root->lchild,vi);
vi.push_back(root->data);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int n,data;
scanf("%d",&n);
node* root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&data);
origin.push_back(data);
insert(root,data);
}
preOrder(root,pre);
preMirrorOrder(root,preM);
postOrder(root,post);
postMirrorOrder(root,postM);
if(pre==origin){
printf("YES\n");
int len=post.size();
for(int i=0;i<len;i++){
printf("%d",post[i]);
if(i!=len-1) printf(" ");
}
}
else if(preM==origin){
printf("YES\n");
int len=postM.size();
for(int i=0;i<len;i++){
printf("%d",postM[i]);
if(i!=len-1) printf(" ");
}
}
else printf("NO\n");
return 0;
}
1043 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 ...
- 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)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 【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 ...
随机推荐
- 【笔记】《深入浅出MFC》第5章 总观Application Framework
凝聚性强.组织化强的类库就是Application Framework.一组合作无间的对象,彼此藉消息的流动而沟通,并且互相调用对方的函数以求完成任务,这就是Application Framework ...
- Java第八次作业--数据库编程
Deadline: 2017-5-18 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握应用JDBC访问数据库的基本步骤 掌握DriverManager类.Connection接 ...
- HDU2037:今年暑假不AC
Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" & ...
- ostringstream的用法
使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中, ...
- (1)变量、常量、程序交互、数据类型、bool、基本运算符
什么是变量 变量由变量名和变量值组成 name = 'Alex Li' 这个算式就是将一个值赋予给变量,也就是声明变量的意思 name 就是一个变量,也是一个变量的名字 'Alex Li' 就是一个 ...
- CTF-练习平台-Misc之 隐写2
二.隐写2 下载文件后解压,发现是一个png图片,依照老套路查看属性,没有发现 用WinHex打开,在图片文件中,修改图片宽度,将箭头处的A改为F,保存后打开图片 发现flag(对于png的文件格式详 ...
- jquery中not的用法[.not(selector)]
描述: 从匹配的元素集合中移除指定的元素. 如果提供的jQuery对象代表了一组DOM元素,.not()方法构建一个新的匹配元素的jQuery对象,用于存放筛选后的元素.所提供的选择器是对每个元素进行 ...
- dockercompose up build fail (node no such file or directory packages.json )
docker构建项目遇到如下问题: npm ERR! Darwin 15.0.0 npm ERR! argv "/usr/local/lib/node_modules/iojs-bin/no ...
- 移植RTL8188CUS USB-WIFI(移植失败)
1.主makefile CONFIG_POWER_SAVING = n CONFIG_PLATFORM_I386_PC = n CONFIG_PLATFORM_HI3518E = y ##swann ...
- 【转】Linux的五个查找命令:find,locate,whereis,which,type
原文网址:http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 最近,我在 ...