文章目录

  • 1、问题描述
    • Input Specification:
    • Output Specification:
      • Sample Input 1:
      • Sample Output 1:
      • Sample Input 2:
      • Sample Output 2:
      • Sample Input 3:
      • Sample Output 3:
  • 2、思路
    • 2.1 二叉排序树
    • 2.2 二叉排序树镜像
  • 3、代码实现

1、问题描述

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 Image of 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 (≤1000). 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

2、思路

2.1 二叉排序树

判断一个二叉树是否为二叉排序树,将给定的先序序列还原成二叉排序树,再得到生成的二叉排序树的先序序列与给定的序列对比,如果相等则给定的序列为二叉排序树。假如给定的序列不是二叉排序树序列,那么生成出来的二叉排序树的先序序列一定和给定的序列不同。

2.2 二叉排序树镜像

判断一个二叉树是否为二叉排序树镜像,思路和上面完全一致,只是在生成的二叉树的先序序列和后续序列时稍微有点不一样。二叉排序树镜像的先序序列为DRL(正常的二叉树为DLR),后序序列为RLD(正常的二叉树为LRD)

3、代码实现

#include<iostream>
#include<vector>using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct node{ElemType data;node * lchild;node * rchild;
}BiNode, *BinTree;
vector<ElemType> Pre,Post,MPre,MPost;void Insert(BinTree &T, ElemType x);
void LDR(BinTree T);
void PreOrder(BinTree T, vector<ElemType> &Pre);
void PostOrder(BinTree T, vector<ElemType> &Post);
void MPreOrder(BinTree T, vector<ElemType> &MPre);
void MPostOrder(BinTree T, vector<ElemType> &MPost);int main(){int n;cin >> n;BinTree T = NULL;ElemType t;vector<ElemType> original;for(int i = 0; i < n; i++){cin >> t;original.push_back(t);Insert(T,t);}PreOrder(T,Pre);PostOrder(T,Post);MPreOrder(T,MPre);MPostOrder(T,MPost);if(original==Pre){cout << "YES" << endl;for(auto it = Post.begin(); it != Post.end(); it++){cout << *(it) << " ";}}else if(original == MPre){cout << "YES" << endl;for(auto it = MPost.begin(); it != MPost.end(); it++){cout << *(it) << " ";}}else{cout << "NO" << endl;}return 0;
}
void Insert(BinTree &T, ElemType  x){if (T == NULL){T = new BiNode ;T->data = x;T->lchild = T->rchild = NULL;}else{if(x < T->data){Insert(T->lchild, x);}else{Insert(T->rchild, x);}}}void LDR(BinTree T){if(T){LDR(T->lchild);cout << T->data << " ";LDR(T->rchild);}
}void PreOrder(BinTree T, vector<ElemType> &Pre){if(T){Pre.push_back(T->data);PreOrder(T->lchild,Pre);PreOrder(T->rchild,Pre);}
}void PostOrder(BinTree T, vector<ElemType> &Post){if(T){PostOrder(T->lchild,Post);PostOrder(T->rchild,Post);Post.push_back(T->data);}
}void MPreOrder(BinTree T, vector<ElemType> &MPre){if(T){MPre.push_back(T->data);MPreOrder(T->rchild, MPre);MPreOrder(T->lchild, MPre);}
}
void MPostOrder(BinTree T, vector<ElemType> &MPost){if(T){MPostOrder(T->rchild,MPost);MPostOrder(T->lchild, MPost);MPost.push_back(T->data);}
}

[PAT A1043]Is is a Binary Search Tree相关推荐

  1. PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...

  2. PAT 甲 1099 Build A Binary Search Tree

    2022.1.28 练习 PAT甲 1099 Build A Binary Search Tree (原题链接) 题解如下: #include <bits/stdc++.h> using ...

  3. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  4. 【PAT】A1099. Build A Binary Search Tree (30)

    Author: CHEN, Yue Organization: 浙江大学 Time Limit: 200 ms Memory Limit: 64 MB Code Size Limit: 16 KB A ...

  5. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

  6. PAT甲级1043 Is It a Binary Search Tree :[C++题解]判断二叉搜索树BST、给定前序序列和中序序列

    文章目录 题目分析 题目链接 题目分析 二叉搜索树(BST):左子树小于根结点,右子树大于等于根结点. 二叉搜索树的中序遍历一定是有序序列.所谓中序遍历:先访问左子树,再访问根结点,最后访问右子树. ...

  7. PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...

  8. 【题意+分析】1043 Is It a Binary Search Tree (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A Binary Search Tree (BST) is recursively defined as a binary tre ...

  9. 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  10. 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)

    议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...

最新文章

  1. OSPF 提升 一 ----基础
  2. java线程状态有哪几种,顺利拿到offer
  3. 10.Azure应用程序网关(上)
  4. angular 路由页面不刷新
  5. python如何更新包_python如何更新包 python更新包代码示例
  6. 批改网禁止粘贴怎么破_教育部对家长批改作业表态了,明令禁止!你怎么看?...
  7. [js] ajax请求地址只支持http/https吗?能做到让它支持rtmp://等其它自定义协议吗 ?
  8. 破坏计算机信息系统功能罪,破坏计算机信息系统罪
  9. Drive Scope for mac(硬盘检查分析工具)
  10. 研发中,问题以界面开发人员为解决负责人
  11. Greedy Mouse 贪心的耗子 nyoj824(贪心算法)
  12. SpringBoot实现导入功能
  13. java 数独 gui,GitHub - fagen/sudoku: 数独终局生成器和GUI
  14. 利用 nodejs 解析 m3u8 格式文件,并下 ts 合并为 mp4
  15. Spring Boot 之---什么是热部署?---怎么使用?
  16. 图像运动模糊及其去除
  17. python英文文本情感分析_sentimentpy模块进行中文文本情感分类
  18. Rime默认为英文状态
  19. SHT30使用的学习过程1SHT30工作模式介绍
  20. 2022中国汽车测试及质量监控博览会

热门文章

  1. 提高网站速度|负载均衡
  2. 【TDA4系列】Linux SDK安装与交叉编译测试,以及刷写SD卡
  3. 递归神经网络的非零初始状态
  4. 自定义分区partitioner实现数据分区存储
  5. Storm目录树、任务提交、消息容错、通信机制
  6. SDNU 1210.通话记录
  7. python——argsort函数
  8. 基本类型和引用类型,执行环境和作用域
  9. Equals() 和 运算符 == 重载准则 (C# 编程指南)
  10. 11. 判断是给属性前加typeof 可以同时判断属性是否存在