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.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search 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. 10
  2. 1 2 3 4 5 6 7 8 9 0

Sample Output:

  1. 6 3 8 1 5 7 9 0 2 4
  2. 题目分析:刚开始我想的是先把末尾的多余的元素 计入计算根节点的位置 但欠缺考虑到对于k层树来说
    若第k层元素大于2k-1 我这样的做法就出了问题
  3. 只过了2个点(21分) //给分真高
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <climits>
  3. #include<iostream>
  4. #include<vector>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<stack>
  9. #include<algorithm>
  10. #include<string>
  11. #include<cmath>
  12. using namespace std;
  13. int Array[];
  14. int Queue[];
  15. void EnQueue(int begin, int end,int pos) //左闭右开
  16. {
  17. if (begin >= end)
  18. return;
  19. Queue[pos] = Array[(begin + end) / ];
  20. EnQueue(begin, (begin + end)/, * pos + );
  21. EnQueue((begin + end) / + , end, * pos + );
  22. }
  23. int main()
  24. {
  25. int N;
  26. cin >> N;
  27. for (int i = ; i < N; i++)
  28. cin >> Array[i];
  29. sort(Array, Array + N);
  30. int sum = ;
  31. for (; sum * < N; sum *= );
  32. int offset = (N - sum)/;
  33. int i = ;
  34. Queue[i] = Array[N / + offset]; //根节点入队
  35. //分别递归处理左右
  36. EnQueue(, N / + offset, * i + );
  37. EnQueue(N / + offset + ,N, * i + );
  38. for (int i = ; i < N - ; i++)
  39. cout << Queue[i] << " ";
  40. if(N!=)
  41. cout << Queue[N- ];
  42. return ;
  43. }

  1. 参考别人的做法
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <climits>
  3. #include<iostream>
  4. #include<vector>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<stack>
  9. #include<algorithm>
  10. #include<string>
  11. #include<cmath>
  12. using namespace std;
  13. int Array[];
  14. int Queue[];
  15. int N;
  16. int id;
  17. void EnQueue(int root) //左闭右开
  18. {
  19. if (root >= N)
  20. return;
  21. EnQueue( * root + );
  22. Queue[root] = Array[id++];
  23. EnQueue( * root + );
  24. }
  25. int main()
  26. {
  27. cin >> N;
  28. for (int i = ; i < N; i++)
  29. cin >> Array[i];
  30. sort(Array, Array + N);
  31. EnQueue();
  32. for (int i = ; i < N - ; i++)
  33. cout << Queue[i] << " ";
  34. cout << Queue[N - ];
  35. return ;
  36. }

来自https://blog.csdn.net/feng_zhiyu/article/details/82219702

果然 很多代码真的是又短又好 虽然原理简单 但体现出的思想正是让我感到震撼

  1.  

1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)的更多相关文章

  1. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  2. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  3. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

  4. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  5. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. PTA 04-树6 Complete Binary Search Tree (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

  7. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

  8. PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  9. 04-树6 Complete Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. 记录:更新VS2019后单元测试运行卡住无法运行测试的问题。

    先说一下是如何遇到这个问题的 今天更新了Visual Studio到最新的版本,然后在运行之前建立的单元测试项目的时候一直卡住,过了一会儿以后提示 未能协商协议,等待响应在 90 秒后超时.出现此问题 ...

  2. 调用系统的loading界面

    //在状态栏显示一个圈圈转动  代表正在请求 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

  3. Flutter 裁剪类组件 最全总结

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 ClipRect ClipRect组件使用矩形裁剪子组件, ...

  4. C++ 函数模板/类模板

    #include <iostream> #include <vector> using namespace std; template < class T > // ...

  5. axios请求拦截器

    import axios from 'axios';   // 创建axios实例   let service = null;   if (process.env.NODE_ENV === 'deve ...

  6. Java中static和final的解析

    static关键字和final关键字是Java中一个难点&重点.本文通过static的用途.常见问题.final的用途.final常见问题,以及static和final的对比来解释这两个关键字 ...

  7. 杂谈 | 增量思维v.s.存量思维

      无挂碍故,无有恐怖,远离颠倒梦想,究竟涅槃. ——<心经>   声明在前,本文并不是要论述“存量思维”是不好的, 而是整理某些场景下需要摒弃“存量思维”,或者提倡“增量思维”.   1 ...

  8. c# winform 访问WebServices 服务(通过WEB引用的方式进行访问)

    第一步.Winform项目引用WEB服务 第二步.代码声明实例化 Web引用 YzServ.TestServ yzserv = new WebYzServ.TestServ(); yzserv.AAA ...

  9. Mac brew命令的使用

    mac 终端程序管理工具 能让你更快速的安装你想要的工具.而不用考虑大量的依赖. 安装brew复制下面的命令,终端执行  官网Homebrew /usr/bin/ruby -e "$(cur ...

  10. VLAN间的通信

    多臂路由/单臂路由 :实现不同VLAN间的通信 1.多臂路由 划分两个vlan,将主机划分到不同vlan中 配置ip地址,(注意:不同vlan在不同的网络下) 将交换机的两个端口分别与路由器连接,将这 ...