1064 Complete 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.

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

 

题意:

  给出一组数字,用这组数字构成一棵完全二叉搜索树。

思路:

  完全二叉搜索树中序遍历的结果,就是升序排序的结果。所以我们可以根据结点的个数,构造出一棵完全二叉搜索树的框架,然后再中序遍历这棵树,将所给的数组排序后依次插入即可。

Code:  

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 typedef struct Node* node;
 6 
 7 struct Node {
 8     int val;
 9     node left;
10     node right;
11     Node(int v) {
12         val = v;
13         left = NULL;
14         right = NULL;
15     }
16 };
17 
18 void buildTree(node& root, int num) {
19     queue<node> que;
20     que.push(root);
21     int count = 1;
22     while (count < num) {
23         node temp = que.front();
24         que.pop();
25         temp->left = new Node(-1);
26         que.push(temp->left);
27         count++;
28         if (count < num) {
29             temp->right = new Node(-1);
30             que.push(temp->right);
31             count++;
32         }
33     }
34 };
35 
36 int tempIndex = 0;
37 void inorderTraveral(node& root, vector<int>& keys) {
38     if (root == NULL) return;
39     inorderTraveral(root->left, keys);
40     root->val = keys[tempIndex++];
41     inorderTraveral(root->right, keys);
42 }
43 
44 void levelTraveral(node& root) {
45     queue<node> que;
46     que.push(root);
47     bool isFirst = true;
48     while (!que.empty()) {
49         node temp = que.front();
50         que.pop();
51         if (isFirst) {
52             cout << temp->val;
53             isFirst = false;
54         } else {
55             cout << " " << temp->val;
56         }
57         if (temp->left != NULL) que.push(temp->left);
58         if (temp->right != NULL) que.push(temp->right);
59     }
60 }
61 
62 int main() {
63     int n;
64     cin >> n;
65     vector<int> keys(n);
66     for (int i = 0; i < n; ++i) cin >> keys[i];
67     sort(keys.begin(), keys.end());
68     node root = new Node(-1);
69     buildTree(root, n);
70     inorderTraveral(root, keys);
71     levelTraveral(root);
72     return 0;
73 }

 

posted @ 2020-05-05 11:14  Veritas_des_Liberty  阅读(158)  评论(0编辑  收藏  举报