1043 Is It a Binary Search Tree (25 分)

与A1102好好对比,都是反转树

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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int maxn = 1010;vector<int> origin, pre, premirror, post, postmirror;struct node {int data;node* lchild;node* rchild;
};void insert(node* &root, int data) {//注意&if (root == NULL) {root = new node;root->data = data;root->lchild = root->rchild = NULL;//别忘了return;}if (data < root->data)insert(root->lchild, data);else insert(root->rchild, data);
}//构建一个BSTvoid preorder(node* root, vector<int>&vi) {//先序遍历构造好的BST并且插入vectorif (root == NULL)return;vi.push_back(root->data);preorder(root->lchild, vi);preorder(root->rchild, vi);}
void preordermirror(node* root, vector<int>&vi) {//先序遍历构造好的BST并且插入vectorif (root == NULL)return;vi.push_back(root->data);preordermirror(root->rchild, vi);preordermirror(root->lchild, vi);}void postorder(node* root, vector<int>&vi) {if (root == NULL)return;postorder(root->lchild, vi);postorder(root->rchild, vi);vi.push_back(root->data);
}
void postordermirror(node* root, vector<int>&vi) {//这里一定要用&if (root == NULL)return;postordermirror(root->rchild, vi);//这里不用&postordermirror(root->lchild, vi);vi.push_back(root->data);
}int main() {int n,data;scanf("%d", &n);node* root =NULL;//切记for (int i = 0; i < n; i++) {scanf("%d", &data);origin.push_back(data);insert(root,data);}preorder(root, pre);preordermirror(root, premirror);postorder(root, post);postordermirror(root, postmirror);if (origin == pre) {printf("YES\n");for (int i = 0; i < n; i++) {printf("%d", post[i]);if (i < n - 1)printf(" ");}}else if (origin == premirror) {printf("YES\n");for (int i = 0; i <n; i++) {printf("%d", postmirror[i]);if (i <n-1)printf(" ");}}else printf("NO\n");return 0;}

1043 Is It a Binary Search Tree (25 分) BST反转?不反转 遍历+vector相关推荐

  1. C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)

    1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary ...

  2. 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 ...

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

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

  4. PTA甲级 1043 Is It a Binary Search Tree (25分) 树的遍历

    强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬 本文由参考于柳神博客写成 柳神的CSDN博客,这个可以搜索文章 柳神的个人博客,这个没有广告,但是不能搜索 还有就是非常非常有用的 算法笔记 全 ...

  5. 1043 Is It a Binary Search Tree (25 分)【难度: 中 / 知识点: 构造二叉搜索树(BST) 】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 首先二叉搜索树是左子树的元素都小于根,左子树 ...

  6. 1043 Is It a Binary Search Tree(二叉查找树BST)

    1043 Is It a Binary Search Tree 0.题目 A Binary Search Tree (BST) is recursively defined as a binary t ...

  7. pat1043. Is It a Binary Search Tree (25)

    1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

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

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

  9. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  10. pat04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

最新文章

  1. 使用admin lte 碰到访问Google字体的问题
  2. 如果只能通过IE写博客【Do we write blog just only with IE?】
  3. Python 基础教程:切片、迭代和列表生成式
  4. AI顶会直播丨深度学习顶级会议ICLR 2021中国预讲会明天召开,为期三天五大论坛...
  5. SDNU 1029.巧分整数(斯特林数(改)dp)
  6. Reporting Service 在文本框中换行的问题
  7. zbrush缝线笔刷制作_Thepoly | 高质量写实人脸制作及实时渲染分享
  8. 骁龙855加持!一加5G原型机将亮相MWC2019:价格却不太友好
  9. 【初级04】JVM线程模型
  10. $().index() 两种用法
  11. API函数的调用过程
  12. 局域网计算机维护工具,局域网共享工具,教您局域网共享工具
  13. 4244. 【五校联考6day2】yi (Standard IO)
  14. C++中如何读取一个数的位数_R语言入门之切尾均值(trimmed mean)和绝对中位差(mad)...
  15. windows下装ipython
  16. 导入d2lzh_pytorch包会出现的问题以及解决方案
  17. 软考中级 真题 2015年上半年 信息系统管理工程师 应用技术
  18. php充值代码,基于php的加油卡充值接口调用代码实例
  19. Unity 之 实用技巧更换编辑器主题
  20. 上海高校计算机等级考试c语言真题,2017年上海市高等学校计算机等级考试试题「有答案」...

热门文章

  1. Redis集群方案介绍
  2. HTML: 和 是何方神圣
  3. oracle:plsql学习总结(oracle database 10g sql 开发指南)
  4. [翻译]Chameleon介绍(6) : 动作控件
  5. QT5.12界面再win10下总是莫名卡死
  6. 【Linux】04 软链接和硬链接
  7. UDP网络编程核心类
  8. 浪涌保护器ant120_浪涌保护器测试流程
  9. linux使用中的问题 --- (Unable to establish SSL connection)
  10. java编写api取数据_Java 8 API 示例:字符串、数值、算术和文件