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. APICloud框架——融云+UIChatTools实现即时通讯聊天

    今天完成了公司app的聊天界面的收发消息功能,结合融云2和UIChatTools模块实现,只是实现了基本功能,好多细节还没有实现,废话不多说,上代码 输入框页面(win) 先引入所需模块 // 融云模 ...

  2. 【书上讲解】最大m段子段和问题

    描述 [题解] 设f[i][j]表示前i个数字分成了j段的最大子段和. 则f[i][j] = max(f[i-1][j]+a[i] (第i个数字和第j段合在一起),f[k][j-1]+a[i] (第i ...

  3. atlcomcli.h(1756): error C2338: CVarTypeInfo< char > cannot be compiled with /J or _CHAR_UNSIGNED fl

    我拿到一个VS的工程,用VS2010 编译 时提示: atlcomcli.h(1756): error C2338: CVarTypeInfo< char > cannot be comp ...

  4. win 解除鼠标右键关联

    点击「开始」→「运行」→「输入Regedit」→「确定」,打开注册表编辑器,找到子键: 「HKEY_CLASSES_ROOT\*\shellex\UltroEdit」,删除此项即可:

  5. 2019杭电多校第一场hdu6579 Operation(线性基)

    Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...

  6. Flink 编程模型

    抽象层次   levels_of_abstraction 最低级的抽象接口是状态化的数据流接口(stateful streaming).这个接口是通过 ProcessFunction 集成到 Data ...

  7. adapter设计模式

    适配器设计模式 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作 例如:现在有一个220V的插口,而手机不能直接接上去,因为锂电 ...

  8. Java多线程sleep和wait的区别,总结得非常好。

    我们都知道sleep是让线程休眠,到时间后会继续执行,wait是等待,需要唤醒再继续执行,那么这两种方法在多线程中的表现形态,它们各有什么区别呢? 可以总结为以下几点. 使用上 从使用角度看,slee ...

  9. gitnore文件修改生效方法

        本文首发于cartoon的博客     转载请注明出处:https://cartoonyu.github.io/cartoon-blog 当修改gitnore文件后,常常出现文件不生效的情况, ...

  10. 1-vim-简介

    vi(visual interface)是Linux最经典的文本编辑器 vi的核心设计思想-让程序员的手指始终保持在键盘的核心区域,就能完成所有的编辑操作. vi的特点 没有图形界面 只能编辑文本内容 ...