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. nuxt 2.0采坑计之 (引入静态文件css)

    nuxt 2.0采坑计之静态文件css 外部引入css 全局引用方法为   (在nuxt.config.js配置中在   module.exports = {}  中添加) head: { meta: ...

  2. 画山 paint

    画山 paint 有一张大小为n*m的白纸,小R想在纸上画一片绵延的群山. 为了描述方便,我们将纸张表示在坐标系上,四个顶点的坐标分别为(0,0),(n,0),(0,m),(n,m). 小R有一只神奇 ...

  3. ASP.NET Core学习——2

    Application Startup ASP.NET Core为应用程序提供了处理每个请求的完整控制.Startup类是应用程程的入口(entry point),这个类可以设置配置(configur ...

  4. linux centos6安装postgresql

    参考:https://blog.csdn.net/zhu_xun/article/details/21234663 参考:https://www.cnblogs.com/jimcsharp/p/857 ...

  5. Codeforces 1169B Pairs

    题目链接:http://codeforces.com/contest/1169/problem/B 题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每 ...

  6. Kali Linux 2018 更新源配置

    查看添加更新源 编辑sources.list,将kali更新源加入其中 sudo vim /etc/apt/sources.list 国内更新源 #阿里云 deb http://mirrors.ali ...

  7. MySQL数据库_目录

    MySQL数据库初识 MySQL的库表详细操作 MySQL行(记录)的详细操作 MySQL之单表查询 MySQL之多表查询 Navicat工具.pymysql模块 MySQL之视图.触发器.事务.存储 ...

  8. web项目中实现页面跳转的两种方式

    <a href="javascript:"></a>跳转在网页本身,URL不改变 <a href="#"></a> ...

  9. 2019基于python的网络爬虫系列,爬取糗事百科

    **因为糗事百科的URL改变,正则表达式也发生了改变,导致了网上许多的代码不能使用,所以写下了这一篇博客,希望对大家有所帮助,谢谢!** 废话不多说,直接上代码. 为了方便提取数据,我用的是beaut ...

  10. python接口自动化(post请求)

    python接口自动化(post请求) 一.post请求的作用:新增资源 二.data格式的参数请求(data是字典对象) #1.导包 import requests #2.调用post方法 #请求的 ...