码迷,mamicode.com
首页 > 其他好文 > 详细

1064. Complete Binary Search Tree (30)

时间:2015-12-06 12:47:14      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

根据中序遍历建立完全二叉树。

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=1000). 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

 

  1. #include <iostream>
  2. #include <vector>
  3. #include<algorithm>
  4. #include<math.h>
  5. #include <queue>
  6. using namespace std;
  7. vector<int> num;
  8. struct Node {
  9. int val;
  10. Node *left;
  11. Node *right;
  12. };
  13. Node* BuildFullTree(int min, int n) {
  14. if (n == 0) {
  15. return NULL;
  16. }
  17. if (n == 1) {
  18. Node *p = (Node*)malloc(sizeof(Node));
  19. p->val = num[min];
  20. p->left = NULL;
  21. p->right = NULL;
  22. return p;
  23. }
  24. int level = 1;
  25. while (true)
  26. {
  27. if (pow(2, level) > n) {
  28. level--;
  29. break;
  30. }
  31. level++;
  32. }
  33. int leftCnt, rightCnt;
  34. if (n + 1 - pow(2, level) < pow(2, level - 1))
  35. leftCnt = n - pow(2, level - 1);
  36. else
  37. leftCnt = pow(2, level) - 1;
  38. rightCnt = n - 1 - leftCnt;
  39. Node *p = (Node*)malloc(sizeof(Node));
  40. p->val = num[min+leftCnt];
  41. p->left = BuildFullTree(min, leftCnt);
  42. p->right = BuildFullTree(min+leftCnt + 1, rightCnt);
  43. return p;
  44. }
  45. int main(void) {
  46. int n;
  47. cin >> n;
  48. for (int i = 0; i < n; i++) {
  49. int temp;
  50. cin >> temp;
  51. num.push_back(temp);
  52. }
  53. sort(num.begin(), num.end());
  54. Node *root = (Node*)malloc(sizeof(Node));
  55. root = BuildFullTree(0, n);
  56. queue<Node*> q;
  57. q.push(root);
  58. while (true)
  59. {
  60. if (q.front()->left != NULL)
  61. q.push(q.front()->left);
  62. if (q.front()->right != NULL)
  63. q.push(q.front()->right);
  64. cout << q.front()->val;
  65. q.pop();
  66. if (q.empty())
  67. break;
  68. cout << " ";
  69. }
  70. return 0;
  71. }





1064. Complete Binary Search Tree (30)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!