Source:

PAT A1043 Is It a Binary Search Tree (25 分)

Description:

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 Imageof 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

Keys:

Code:

 /*
Data: 2019-06-26 17:25:17
Problem: PAT_A1043#Is It a Binary Search Tree
AC: 26:46 题目大意:
BST定义:lchild < root <= rchild
镜像树:交换左右子树
现给定一个序列,判断其是否为BST或Mirror BST的先序遍历,若是,打印后序遍历 基本思路:
由先序序列可以构建一棵唯一的二叉树,
分别NLR和NRL遍历该二叉树,若遍历序列与给定序列相同,则Yes,反之No
*/
#include<cstdio>
#include<vector>
using namespace std;
vector<int> pre,post,input;
struct node
{
int data;
node *lchild,*rchild;
}; void Insert(node *&root, int x)
{
if(root == NULL)
{
root = new node;
root->data = x;
root->lchild = root->rchild = NULL;
}
else if(x < root->data)
Insert(root->lchild, x);
else
Insert(root->rchild, x);
} void NLR(node *root)
{
if(root == NULL)
return;
pre.push_back(root->data);
NLR(root->lchild);
NLR(root->rchild);
post.push_back(root->data);
} void NRL(node *root)
{
if(root == NULL)
return;
pre.push_back(root->data);
NRL(root->rchild);
NRL(root->lchild);
post.push_back(root->data);
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,x;
scanf("%d", &n);
node *root = NULL;
for(int i=; i<n; i++)
{
scanf("%d", &x);
Insert(root, x);
input.push_back(x);
}
NLR(root);
if(pre == input)
{
printf("YES\n");
for(int i=; i<post.size(); i++)
printf("%d%c", post[i],i==post.size()-?'\n':' ');
}
else
{
pre.clear();
post.clear();
NRL(root);
if(pre == input)
{
printf("YES\n");
for(int i=; i<post.size(); i++)
printf("%d%c", post[i],i==post.size()-?'\n':' ');
}
else
printf("NO\n");
} return ;
}

PAT_A1043#Is It a Binary Search Tree的更多相关文章

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

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

  2. Leetcode 笔记 99 - Recover Binary Search Tree

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

  3. Leetcode 笔记 98 - Validate Binary Search Tree

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

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

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

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. 以字符集为位数的字符串hash——上海网络赛G

    先预处理一个hash[a][b]:开头字符为a, 结尾字符是b,中间的字符hash值为hs的的hash表,保存的是出现次数 对于一个子串求hash值的策略:设hash值是个26位的数,每新增一个字符, ...

  2. cf期望概率专题

    cf1009E:求到第i段期望和的比较困难,但是单独求每段的期望是比较容易的,所以单独对每段求和,然后累计总和 E[i]=1/2*a1+1/4*a2+...+1/2^(i-1)*ai-1+1/2^(i ...

  3. js设计模式——5.状态模式

    js设计模式——5.状态模式 代码演示 /*js设计模式——状态模式*/ // 状态(红灯,黄灯,绿灯) class State { constructor(color) { this.color = ...

  4. (转)阿里RocketMQ Quick Start

    转:http://blog.csdn.net/a19881029/article/details/34446629 RocketMQ单机支持1万以上的持久化队列,前提是足够的内存.硬盘空间,过期数据数 ...

  5. mybatis执行test07测试类却显示test05测试类调用的sql语句出错

    1.测试类 @Test public void test07() { IStudentDao studentDao = new IStudentDaoImpl(); Student student = ...

  6. testNG官方文档翻译-2 注解

    这里是一份TestNG中的可用注解及其属性的概述. 一.用于一个TestNG类的信息配置的注解: @BeforeSuite:被BeforeSuite注解的方法将在其所在suite中的所有test运行之 ...

  7. 【Linux】- Systemd 实战篇

    转自:阮一峰的网络日志 一.开机启动 对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件. 如果你想让该软件开机启动,就执行下 ...

  8. java-day02

    数据类型自动转换 要求:数据范围从小到大 数据类型强制类型转换 格式:范围小的数据类型 范围小的变量名 = (范围小的数据类型)原范围大的数据 注意事项: 1.可以会造成数据溢出或者是精度损失. 2. ...

  9. 使ie8正常支持placeholder

    在IE8下测试,发现一个问题placeholder不被支持,下面是解决IE支持placeholder的方法,本文引用的jquery是1.12.0测试通过,先引用jquery <script ty ...

  10. 8-vim-移动命令-03-段落切换和括号切换

    1.段落切换 vi使用空行来区分段落 在程序开发时,通常一段功能相关的代码会写在一起--之间没有空行 命令 功能 { 上一段 } 下一段 2.括号切换 在程序世界中,() [] {}使用频率很高,而且 ...