• 【PAT(甲级)】1043 Is It a Binary Search Tree


    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

    解题思路:

    二叉搜索树符合以下三点:

    • 所有左子树的节点都小于根节点
    • 所有右子树的节点都大于等于根节点
    • 左右子树都符合二叉搜索树的条件

    镜像先序就是:根,右子树,左子树;

    镜像后序就是:右子树,左子树,根;

    先序就是:根,左子树,右子树;

    后序就是:左子树,右子树,根。

    题目给出树的先序输出或者是镜像先序输出,如果不是则输出NO,是的话就输出YES以及对应的后序输出。首先我们构造一棵树,然后用四个相应的数组来存储先序,镜像先序,后序,镜像后序的顺序来读取的这棵树。如果与输入的数组顺序相符合,就可以输出对应的后序了。

    易错点:

    1. 右子树大于等于根节点;

    2. 关于建树的一些问题,指针啥的。

    代码:

    1. #include
    2. using namespace std;
    3. typedef struct Tree{
    4. int node;
    5. Tree *left;
    6. Tree *right;
    7. };
    8. int N;
    9. vector<int> t,pre,mpre,post,mpost;
    10. void read(Tree* &a,int b){//读取数据来建树
    11. if(a == NULL){
    12. a = new Tree;
    13. a->node = b;
    14. a->left = NULL;
    15. a->right = NULL;
    16. return;
    17. }
    18. else if(b>=a->node){
    19. read(a->right,b);
    20. }
    21. else{
    22. read(a->left,b);
    23. }
    24. }
    25. void preorder(Tree *a){
    26. if(a!=NULL){
    27. pre.push_back(a->node);
    28. preorder(a->left);
    29. preorder(a->right);
    30. }
    31. }
    32. void mpreorder(Tree *a){
    33. if(a != NULL){
    34. mpre.push_back(a->node);
    35. mpreorder(a->right);
    36. mpreorder(a->left);
    37. }
    38. }
    39. void postorder(Tree *a){
    40. if(a!=NULL){
    41. postorder(a->left);
    42. postorder(a->right);
    43. post.push_back(a->node);
    44. }
    45. }
    46. void mpostorder(Tree *a){
    47. if(a!=NULL){
    48. mpostorder(a->right);
    49. mpostorder(a->left);
    50. mpost.push_back(a->node);
    51. }
    52. }
    53. int main(){
    54. Tree *tree = NULL;
    55. cin>>N;
    56. for(int i=0;i
    57. int x;
    58. cin>>x;
    59. t.push_back(x);
    60. read(tree,x);
    61. }
    62. preorder(tree);
    63. mpreorder(tree);
    64. postorder(tree);
    65. mpostorder(tree);
    66. if(t == pre){
    67. cout<<"YES"<
    68. for(auto i=post.begin();i!=post.end();i++){
    69. if(i==post.begin())
    70. cout<<*i;
    71. else
    72. cout<<" "<<*i;
    73. }
    74. }
    75. else if(t == mpre){
    76. cout<<"YES"<
    77. for(auto i=mpost.begin();i!=mpost.end();i++){
    78. if(i==mpost.begin())
    79. cout<<*i;
    80. else
    81. cout<<" "<<*i;
    82. }
    83. }
    84. else{
    85. cout<<"NO";
    86. }
    87. return 0;
    88. }

  • 相关阅读:
    WEB前端网页设计 HTML CSS 网页设计参数 - 【高度坍塌】
    pandas数据分析综合练习50题 - 地区房价分析
    云原生系列四:Yelp 如何在 Kubernetes 上运行 Kafka
    esp8266 Task任务创建与执行
    计算机竞赛 深度学习乳腺癌分类
    如何避免JavaScript中的内存泄漏?
    交互式 Web 应用 0 基础入门
    优维产品最佳实践第14期:让重要告警能有序跟进,最终根治
    docker-compose概述与简单编排部署
    zlMediaKit 1 task模块--怎么用异步做到同步,怎么基于任务而非基于线程管理
  • 原文地址:https://blog.csdn.net/weixin_55202895/article/details/126463625