首页 > 其他 > 详细

1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

时间:2019-12-18 23:12:53      阅读:121      评论:0      收藏: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.

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:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

题目分析:刚开始我想的是先把末尾的多余的元素 计入计算根节点的位置 但欠缺考虑到对于k层树来说
若第k层元素大于2的k-1次 我这样的做法就出了问题

只过了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[1010];
14 int Queue[1010];
15 void EnQueue(int begin, int end,int pos) //左闭右开
16 {
17     if (begin >= end)
18         return;
19     Queue[pos] = Array[(begin + end) / 2];
20     EnQueue(begin, (begin + end)/2, 2 * pos + 1);
21     EnQueue((begin + end) / 2 + 1, end, 2 * pos + 2);
22 }
23 int main()
24 {
25     int N;
26     cin >> N;
27     for (int i = 0; i < N; i++)
28         cin >> Array[i];
29     sort(Array, Array + N);
30     int sum = 1;
31     for (; sum * 2 < N; sum *= 2);
32     int offset = (N - sum)/2;
33     int i = 0;
34     Queue[i] = Array[N / 2 + offset]; //根节点入队
35     //分别递归处理左右
36     EnQueue(0, N / 2 + offset, 2 * i + 1);
37     EnQueue(N / 2 + offset + 1,N,2 * i + 2);
38     for (int i = 0; i < N - 1; i++)
39         cout << Queue[i] << " ";
40     if(N!=0)
41         cout << Queue[N- 1];
42     return 0;
43 }
View Code

参考别人的做法
技术分享图片
 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[1010];
14 int Queue[1010];
15 int N;
16 int id;
17 void EnQueue(int root) //左闭右开
18 {
19     if (root >= N)
20         return;
21     EnQueue(2 * root + 1);
22     Queue[root] = Array[id++];
23     EnQueue(2 * root + 2);
24 }
25 int main()
26 {
27     cin >> N;
28     for (int i = 0; i < N; i++)
29         cin >> Array[i];
30     sort(Array, Array + N);
31     EnQueue(0);
32     for (int i = 0; i < N - 1; i++)
33         cout << Queue[i] << " ";
34     cout << Queue[N - 1];
35     return 0;
36 }
View Code

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

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

 

1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

原文:https://www.cnblogs.com/57one/p/12063555.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!