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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

随机推荐

  1. day 7-7 线程池与进程池

    一. 进程池与线程池 在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多,这 ...

  2. CRM系统数据授权

    1.新建角色,华东二区 2.业务对象中找到客户管理 3.在数据范围中新建数据规则,并进行设置 4.点击授权后,生效. 另:数据权限设置

  3. CSS实现元素水平垂直居中

    我们知道,实现元素的水平居中比较简单,在设置了宽度后,设置左右margin为auto就可以. 但是如何设置元素垂直居中呢? 当然,对于单行的文字,可以通过设置line-height来解决, 可以对于一 ...

  4. jquery选择基础

    1 元素选择器 之前不熟悉的是如: $("input.cls1"); 这种用法 2 属性选择器 包含name属性的input元素, 如 $("input[name]&qu ...

  5. nginx反向代理proxy_pass的问题

    起因:今天企业部署一个项目,用的nginx做的反向代理,配置如下: 测试结果令人失望,IP:端口 能访问项目,域名:端口 也能访问 ,但是 域名/接口名 访问失败 ################## ...

  6. Reversing-x64Elf-100

    一道很简单的小题 作为python小白这道题主要是学习了一点python知识...... 可以看出来 sub_4006FD 这个函数是用来判断输入密码是否正确的 我们看一下它的伪代码: signed ...

  7. NaN与Null与undefiined的关系

    在js中,定义一个变量需要通过关键字var来定义,定义的变量可以是字符串.数字等等都行.但是如果你只是定义了一个变量,没有给他赋值,那么它就默认为'undefined'.例如 var name; co ...

  8. UVA 1602 Lattice Animals

    题目 输入n.w.h($1\leqslant n \leqslant 10, 1\leqslant w,h \leqslant n$),求能放在w*h网格里的不同的n连块的个数(注意,平移.旋转.翻转 ...

  9. mysql 集群方案

    试试基于Galera的MySQL高可用集群  mha  mgr

  10. [SDOI2015] 寻宝游戏

    传送门:>Here< 题意:给出一棵树(有边权),刚开始键值全部为0.每次对其中一个键值进行异或,问每一次修改之后:选择任意一个点出发走到所有为1的点再走回来的最短路 解题思路 由于N,M ...