• 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. }

  • 相关阅读:
    手写 Vue2 系列 之 初始渲染
    LangChain大型语言模型(LLM)应用开发(三):Chains
    Java实战:Spring Boot热部署DevTools使用
    PB 变量命名规范
    nginx配置参数详细解析
    Java毕业设计之评教评分教务管理系统springboot mybatis
    【Plus】三、BaseMapper
    数据结构排序算法---八大排序复杂度及代码实现
    Java 线程
    车载测试中:如何处理 bug
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127806552