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. CSS:CSS 伪类(Pseudo-classes)

    ylbtech-CSS:CSS 伪类(Pseudo-classes) 1.返回顶部 1. CSS 伪类(Pseudo-classes) CSS伪类是用来添加一些选择器的特殊效果. 语法 伪类的语法: ...

  2. 3、获取APP 内存占用率

    关于APP内存占用,不用多说,应该是APP性能测试中比较重要的一点.试想一下,开个应用把手机内存占满了,其它应用无法打开,那么这个应用还会有人安装吗?我觉得是没有的.下面就通过adb命令获取APP虚存 ...

  3. C语言——二维数组

    目录 二维数组 一.二维数组的定义 二.二维数组的初始化 三.通过赋初值定义二维数组的大小 四.二维数组与指针 二维数组 一.二维数组的定义 类型名 数组名[ 常量表达式1 ][ 常量表达式2 ] i ...

  4. python调用tushare获取A股周线行情

    接口:weekly 描述:获取A股周线行情 限量:单次最大3700,总量不限制 积分:用户需要至少300积分才可以调取,具体请参阅本文最下方积分获取办法 注:tushare模块下载和安装教程,请查阅我 ...

  5. jsk

    题目描述 码队的女朋友非常喜欢玩某款手游,她想让码队带他上分.但是码队可能不会带青铜段位的女朋友上分,因为码队的段位太高(已经到达王者),恐怕不能和他的女朋友匹配游戏. 码队的女朋友有些失落,她希望能 ...

  6. (转)Java中Image的水平翻转、缩放与自由旋转操作

    来自:http://cping1982.blog.51cto.com/601635/130066/ 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责 ...

  7. ida 符号路径设置

    cfg/pdf.cfg 文件中主要有两个字段 PDBSYM_DOWNLOAD_PATH 这个字段可以注释掉 PDBSYM_SYMPATH 这个字段需要设置一个符号路径,具体设置方法和WinDBG的设置 ...

  8. Spring开发案例1半注解开发

    dao层: package cn.mepu.dao.imp; import cn.mepu.dao.AccountDao; import cn.mepu.domain.Account; import ...

  9. 2018-2-13-win10-uwp-入门

    title author date CreateTime categories win10 uwp 入门 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...

  10. wbinfo - 向winbind服务查询信息

    总览 SYNOPSIS wbinfo [-a user%password] [-c username] [-C groupname] [--domain domain] [-I ip] [-s sid ...