1043 Is It a Binary Search Tree

0、题目

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

1、大致题意

给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或其镜像进行前序遍历的结果。

2、基本思路

2.1 二叉排序树

二叉排序树有以下的性质:

  • 左边结点的值比其父节点的值小
  • 右边结点的值大于或等于其父节点的值
  • 左子树和右子树都必须是二叉树

2.1.1 二叉排序树的镜像

如果交换左右结点的值,那么形成的二叉排序树就是原二叉排序树的镜像。现给出一串整数序列,你需要判断该序列是否是一棵二叉排序树或二叉排序树的镜像的先序序列,并输出其后序序列。

2.2 解题思路

首先边输入结点的值边插入形成一棵二叉排序树,见函数build()

接着对构建好的二叉排序树进行先序preOrder()、镜像先序mirrorPreOrder()和后序postOrder()、镜像后序mirrorPostOrder()遍历;

最后判断题目给出的序列和先序遍历结果、镜像先序遍历结果是否一致,输出结果即可。

  • 先序遍历:根节点、左子树、右子树(中左右)
  • 镜像先序遍历:根节点、右子树、左子树(中右左)
  • 后序遍历:左子树、右子树、根节点(左右中)
  • 镜像后序遍历:右子树、左子树、根节点(右左中)

2.3 补充-C++中如何判断两个vector是否相等?

直接使用==

如果vector里面的元素类型是简单类型(内置类型),可以直接使用==或者!=进行比较
因为在STL里面,==!=是可以直接使用的:

template< class T, class Alloc >
bool operator==( const vector<T,Alloc>& lhs,const vector<T,Alloc>& rhs );template< class T, class Alloc >
bool operator!=( const vector<T,Alloc>& lhs,const vector<T,Alloc>& rhs );

甚至可以使用<=<>=>比较两个vector大小:按照字典序排列

template< class T, class Alloc >
bool operator<( const vector<T,Alloc>& lhs,const vector<T,Alloc>& rhs );template< class T, class Alloc >
bool operator<=( const vector<T,Alloc>& lhs,const vector<T,Alloc>& rhs );template< class T, class Alloc >
bool operator>( const vector<T,Alloc>& lhs,const vector<T,Alloc>& rhs );template< class T, class Alloc >
bool operator>=( const vector<T,Alloc>& lhs,const vector<T,Alloc>& rhs );

3、AC代码

#include<iostream>
#include<vector>
using namespace std;
struct Node{int data;Node *left,*right;
};
int N;
vector<int> origin,pre,preMirror,post,postMirror;
Node *tree = NULL;
int sum = 0;
//构建二叉排序树
void build(Node* &node,int data){sum ++;if(node == NULL){node = new Node;node->data = data;node->left = node->right = NULL;return;}if(data < node->data)build(node->left,data);elsebuild(node->right,data);
}
//先序遍历
void preOrder(Node* tree){if(tree == NULL)return;pre.push_back(tree->data);preOrder(tree->left);preOrder(tree->right);
}
//先序遍历镜像
void mirrorPreOrder(Node *tree){if(tree == NULL)return;preMirror.push_back(tree->data);mirrorPreOrder(tree->right);mirrorPreOrder(tree->left);
}
//后序遍历
void postOrder(Node *tree){if(tree == NULL)return;postOrder(tree->left);postOrder(tree->right);post.push_back(tree->data);
}
//后序遍历镜像
void mirrorPostOrder(Node *tree){if(tree == NULL)return;mirrorPostOrder(tree->right);mirrorPostOrder(tree->left);postMirror.push_back(tree->data);
} int main(){scanf("%d",&N);for(int i = 0 ; i < N ; i ++){int temp;scanf("%d",&temp);origin.push_back(temp);build(tree,origin[i]);//构建二叉排序树 }preOrder(tree);mirrorPreOrder(tree);postOrder(tree);mirrorPostOrder(tree);if(pre == origin){//初始序列等于先序序列 printf("YES\n");for(int i = 0 ; i < post.size() ; i ++){if(i > 0)printf(" ");printf("%d",post[i]);}}else if(preMirror == origin){//初始序列等于先序镜像序列 printf("YES\n");for(int i = 0 ; i < postMirror.size() ; i ++){if(i > 0)printf(" ");printf("%d",postMirror[i]);}}else{printf("NO");}return 0;
}

1043 Is It a Binary Search Tree(二叉查找树BST)相关推荐

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

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

  2. BST(Binary Search Tree 二叉查找树模版)

    /****************************************** 数据结构: BST(Binary Search Tree),二叉查找树;性质: 若结点的左子树不空,则左子树上所 ...

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

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

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

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

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

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

  7. 1043 Is It a Binary Search Tree

    1. 这是二叉查找树BST那节的习题,要做出来这题,需要具备的基础知识有:BST的创建,涉及函数createBST,insertBST,newNode,二叉树的先序遍历.后序遍历. 2. 需要转过来的 ...

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

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

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

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

最新文章

  1. 学计算机应用好还是汽车维修好,大学汽车运用与维修专业怎么样_学什么_前景好吗-520吉他网...
  2. github登陆策略
  3. linux shell awk 中括号 方括号 分割 []
  4. django 模板语言之 simple_tag 自定义模板
  5. 组会20211008《kEMPO1粒子模拟核心代码注释》
  6. DataSet DataTable操作
  7. Spring 3.1:缓存和EhCache
  8. matlab jp2格式,JP2文件扩展名_JP2是什么格式_JP2文件怎么打开-文件百科
  9. c 子类对象 访问父类对象受保护成员_java面向对象总结
  10. MITx - 6.00.1x 笔记(4) Good Programming Practices
  11. daily-question-01(前端每日一题01)
  12. 基于JAVA+SSH+MYSQL的鲜花订购系统
  13. OPENSSL编程 第二十章 椭圆曲线
  14. 20155330 第十一周课堂练习(20170503)
  15. 基于 USB 传输的针式打印机驱动程序开发
  16. Python 16进制、字符串转换
  17. Excel高级图表实现
  18. Excel的基本操作(一):保护功能、快速输入数据、导入txt数据和导入Access数据库、Excel数据类型等基本操作
  19. 【转】非教育网中IPv4网络访问IPv6资源
  20. Java多线程基础(下)

热门文章

  1. 传输视频的带宽如何计算?传输4K视频需要多少带宽?
  2. js算法:杨辉三角(帕斯卡三角)
  3. Centos8安装MySQL8.0,基于rpm安装
  4. 使用RSS地址订阅微信公众账号的文章~
  5. 开发一款可以点击头像图片打电话的安卓软件
  6. IO与集合习题综合练习
  7. 【修改照片gps信息】向照片写入位置gps信息
  8. Hydra(暴力破解)
  9. python开发跟淘宝有关联微_python - 爬取某淘宝店铺所有宝贝遇到的问题?
  10. 使用wget下载需要用户名和密码访问的网站资源