• 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:

    1. 7
    2. 8 6 5 7 10 8 11

    Sample Output 1:

    1. YES
    2. 5 7 6 8 11 10 8

    Sample Input 2:

    1. 7
    2. 8 10 11 8 6 7 5

    Sample Output 2:

    1. YES
    2. 11 8 10 7 5 6 8

    Sample Input 3:

    1. 7
    2. 8 6 8 5 10 9 11

    Sample Output 3:

    NO

    题目大意

    给你一串数组,判断这是否是对⼀棵⼆叉搜索树或其镜像翻转后,进⾏前序遍历的结果
    是的话输出 YES + 该树的后续遍历
    否则 NO

    思路

    分类讨论是否为镜像翻转


    C/C++ 

    1. #include
    2. using namespace std;
    3. vector<int> tree1,tree2;
    4. int N,num;
    5. bool flag = true; // 判断有无镜像翻转
    6. void create(int head,int tail);
    7. int main()
    8. {
    9. cin >> N;
    10. for(int z=0;z
    11. cin >> num;
    12. tree1.push_back(num);
    13. }
    14. create(0,N-1);
    15. if(tree2.size()!=tree1.size()){
    16. tree2.clear();
    17. flag = false;
    18. create(0,N-1);
    19. }
    20. if(tree2.size()!=tree1.size()) puts("NO");
    21. else
    22. {
    23. cout << "YES" << endl << tree2[0];
    24. for(int z=1;z" " << tree2[z];
    25. }
    26. return 0;
    27. }
    28. void create(int head,int tail)
    29. {
    30. if(head>tail) return;
    31. int left=head+1,right=tail;
    32. if(flag){
    33. while (left<=tail && tree1[head]>tree1[left]) left++;
    34. while (right>head && tree1[head]<=tree1[right]) right--;
    35. }else{
    36. while (left<=tail && tree1[head]<=tree1[left]) left++;
    37. while (right>head && tree1[head]>tree1[right]) right--;
    38. }
    39. if(left-right>1) return;
    40. create(head+1,left-1);
    41. create(right+1,tail);
    42. tree2.push_back(tree1[head]);
    43. }

  • 相关阅读:
    ✨Nifi系列✨ Nifi同步并解析FTP服务器Excel文件,并存储到数据库
    android源码添加c/c++工程
    基于STM32单片机一氧化碳(CO)气体监控系统proteus仿真设计
    MySQL创建数据表(CREATE TABLE语句)
    鸿蒙面试心得
    Anconda环境中python默认不是该环境下的python
    LCD电子手提秤电路板方案软硬件开发
    从源码层解读react渲染原理
    python数据分析(numpy)
    小米6安装Ubuntu Touch系统也不是很难嘛
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127806552