A1043. Is It a Binary Search Tree
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 (<=1000). 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>
#include<iostream>
#include<vector>
using namespace std;
typedef struct NODE{
struct NODE *lchild, *rchild;
int key;
}node;
int N;
vector<int> keys, pre, preM, ans;
void insert(node* &root, int key){
if(root == NULL){
root = new node;
root->key = key;
root->lchild = NULL;
root->rchild = NULL;
return;
}
if(key >= root->key){
insert(root->rchild, key);
}else{
insert(root->lchild, key);
}
}
node* create(vector<int> &keys){
node* root = NULL;
for(int i = ; i < N; i++)
insert(root, keys[i]);
return root;
}
void preOrder(node* root){
if(root == NULL)
return;
pre.push_back(root->key);
preOrder(root->lchild);
preOrder(root->rchild);
}
void preOrder2(node* root){
if(root == NULL)
return;
preM.push_back(root->key);
preOrder2(root->rchild);
preOrder2(root->lchild);
}
void postOrder(node* root){
if(root == NULL)
return;
postOrder(root->lchild);
postOrder(root->rchild);
ans.push_back(root->key);
}
void postOrder2(node* root){
if(root == NULL)
return;
postOrder2(root->rchild);
postOrder2(root->lchild);
ans.push_back(root->key);
} int main(){
int temp;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &temp);
keys.push_back(temp);
}
node* root = create(keys);
preOrder(root);
preOrder2(root);
if(pre == keys ){
printf("YES\n");
postOrder(root);
for(int i = ; i < N; i++){
if(i != N - )
printf("%d ", ans[i]);
else printf("%d", ans[i]);
}
}else if(preM == keys){
printf("YES\n");
postOrder2(root);
for(int i = ; i < N; i++){
if(i != N - )
printf("%d ", ans[i]);
else printf("%d", ans[i]);
}
}else {
printf("NO\n");
}
cin >> N;
return ;
}
总结:
1、题意:给出一组key,先按照给出的顺序建立搜索树。再对其本身和他的镜像进行先序遍历,看看是否先序遍历的序列和给出的一组key顺序相同。
2、对逆转的镜像树,可以不必实际上逆转该树,而仅仅在先序和后序访问左右子树时,从原来的先左后右变成先右后左。
3、两个vector在元素为int时可以直接比较。
4、注意字符串不要打错,“NO”打成“No”结果检查好久。
5、本题中允许搜索树中有重复的key,在定义中右子树大于等于根节点。 recursively:递归地
A1043. Is It a Binary Search Tree的更多相关文章
- A1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- A1043 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甲级——A1043 Is It a Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT_A1043#Is It a Binary Search Tree
Source: PAT A1043 Is It a Binary Search Tree (25 分) Description: A Binary Search Tree (BST) is recur ...
- 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 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
随机推荐
- freemarker 简单操作
操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end end: 截取子串的长度,end必须大于 ...
- python学习笔记(6)--条件分支语句
if xxxx: coding if xxxx: coding else: coding if xxxx: coding elif xxx: coding …… else: coding 或者一种简洁 ...
- nfs+keepalived高可用
1台nfs主被服务器都下载nfs.keepalived yum install nfs-utils rpcbind keepalived -y 2台nfs服务器nfs挂载目录及配置必须相同 3.在主n ...
- AI算法第一天【概述与数学初步】
1. 机器学习的定义: 机器从数据中学习出规律和模式,以应用在新数据上作出预测的任务 2.学习现象: (1)语言文字的认知识别 (2)图像,场景,物体的认知和识别 (3)规则:下雨天要带雨伞 (4)复 ...
- Windows Server 2012 IIS 8 - 安装SSL证书
从证书邮件里或者用户中心复制对应的SSL证书文件代码 把代码粘贴到TXT文本文件里面 然后另存为cer或是crt文件,注意编码为ANSI 中级证书和交叉证书也是按以上方法保存为crt或cer文件即可 ...
- maven加载本地jar包到repository
maven加载本地jar到repository 这是一个常见场景,此处以本地opencv jar文件导入repository为例 1.Ubuntu下 mvn install:install-file ...
- 第二十二天 logging hashlib re 模块
今日内容 logging功能完善的日志模块 re正则表达式模块主要处理字符串匹配 查找 搜索给你一个字符串 要从中找到你需要的东西 爬虫大量使用 hashlib hash算法相关的库 算法怎么算不需要 ...
- Elasticsearch 聚合统计与SQL聚合统计语法对比(一)
Es相比关系型数据库在数据检索方面有着极大的优势,在处理亿级数据时,可谓是毫秒级响应,我们在使用Es时不仅仅进行简单的查询,有时候会做一些数据统计与分析,如果你以前是使用的关系型数据库,那么Es的数据 ...
- centos 7创建ss服务(方式二)
一:安装pip yum install python-pip 如果没有python包则执行命令:yum -y install epel-release: 二:安装SS pip install shad ...
- SQLSERVER 维护计划无法删除
数据对网站运营或者企业运营是至关重要的,所以,我们在使用数据库的时候,为了保证数据的安全可靠性,都会做数据库备份,很显然,这个备份,我们不可能每天都去手动备份,SQLServer 数据库就可以提供数据 ...