PAT甲级——A1064 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

 

It may be noted by observing, for any node among the complete binary tree (set number x), with the number of children left must be 2x, and the right number of the child must be 2x + 1. That is, the complete binary tree can 2k array to store the information of all the nodes through the establishment of a size, where k is the maximum height of a complete binary tree, and the number must be stored in a root node (think about why root knot point can not exist at index 0?). Such an array can be used to index the table 95 in FIG complete binary tree node ID number shows a schematic, left child and right child, and the number can be obtained are calculated directly.
In fact, if not a complete binary tree, you can also view them as a complete binary tree, that is, the empty node also actual numbers work. But by doing this the whole tree is space consuming huge one strand (k-nodes need of size 2 k array), thus this method is rarely used to store the tree general nature. But if the title is already provided for complete binary tree, then the array size need only set the upper limit of the number of nodes plus 1 can be, which will greatly reduce the coding complexity.
In addition, the order of elements in the array stored in exactly the sequence that complete binary tree traversal sequence. And determining whether a node is a leaf node flag: the total of the node (referred to as a subscript root) of the left child node of the root * 2 number greater than the number of nodes n (do not need to think about why Analyzing ? right child node); a flag determining whether the node is the null node: the node index root node is greater than the total number n.

 

. 1 #include <the iostream>
 2 #include <Vector>
 . 3 #include <algorithm>
 . 4  the using  namespace STD;
 . 5  int N, the nums [ 1001 ], RES [ 1001 ], index = 0 ;
 . 6  void levelOrder ( int K)
 . 7  {
 . 8      IF (K> N) // leaf node 
. 9          return ;
 10      levelOrder (K * 2 ); // traverse the left subtree 
. 11      RES [K] = the nums [index ++]; // That is, after the left subtree been traversed, this when is the root node 
12     levelOrder(k * 2 + 1);//遍历右子树
13 }
14 int main()
15 {
16     cin >> N;
17     for (int i = 0; i < N; ++i)
18         cin >> nums[i];
19     sort(nums, nums + N, [](int a, int b) {return a < b; });
20     levelOrder(1);
21     for (int i = 1; i <= N; ++i)
22         cout << res[i] << (i == N ? "" : " ");
23     return 0;
24 }

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11295343.html