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. 【Java架构:基础技术】一篇文章搞掂:Eclipse

    Eclipse中使用SVN 1.打开资源库视图 https://www.cnblogs.com/liangguangqiong/p/7965770.html 一.编辑器方面 格式化取消自动换行:打开E ...

  2. MySql在建立索引优化时需要注意的问题

    MySql在建立索引优化时需要注意的问题 设计好MySql的索引可以让你的数据库飞起来,大大的提高数据库效率.设计MySql索引的时候有一下几点注意: 1,创建索引 对于查询占主要的应用来说,索引显得 ...

  3. ASP.NET Core学习——1

    ASP.NET Core介绍 ASP.NET Core是一个新的开源和跨平台的框架,用于构建如Web应用.物联网(IoT)应用和移动后端应用等连接到互联网的基于云的现代应用程序.ASP.NET Cor ...

  4. 前端(十二)—— JavaScript基础操作:if语句、for循环、while循环、for...in、for...of、异常处理、函数、事件、JS选择器、JS操作页面样式

    JavaScript基础操作 一.分支结构 1.if语句 if 基础语法 if (条件表达式) { 代码块; } // 当条件表达式结果为true,会执行代码块:反之不执行 // 条件表达式可以为普通 ...

  5. Java缓冲区读写

    缓冲区读 有两种方法从缓冲区读取数据: 绝对位置 相对位置 使用四个版本重载的get()方法用于从缓冲区读取数据. get(int index)返回给定索引处的数据. get()从缓冲区中的当前位置返 ...

  6. 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷

    通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下:     步骤1:通过MyEclipse中的window->show View->ot ...

  7. 【Linux】- Systemd 命令篇

    转自:阮一峰的网络日志 Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置. 一.由来 历史上,Linux 的启动一直采用init进程. 下面的命令用来启动服务. ...

  8. Delphi(ObjectPascal)基础语法

      一个程序分为两个部分:1.程序首部:program 来标识这是一个pascal程序  后面的是可执行文件的名称程序名称2.程序体:说明部分:数据先定义后使用执行部分:以begin开始,以end结束 ...

  9. leetcode.数组.16最接近的三数之和-java

    1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...

  10. idea springboot 打包 war

    1.pom文件中将项目改为   war