A binary Search Tree (BST) is recursively defined as a Binary Tree which have 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 is binary search trees.
A complete Binary tree (CBT) was a tree that was completely filled, with the possible exception of the bottom level, which I s filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can being constructed if it is required that the Tre E must also be a CBT. You is supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains the one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys is given in the next line. All the numbers in a line is separated by a space and is 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 is separated by a space, and there must is no extra space at the end of the line.
Sample Input:
101 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
1#include <stdio.h>2#include <stdlib.h>3#include <iostream>4#include <string.h>5#include <math.h>6#include <algorithm>7#include <string>8#include <stack>9#include <queue>Ten using namespacestd; One Const intmaxn=1010; A intN,NUMBER[MAXN],CBT[MAXN]; - intShuzucount=0; - the voidInorder (intRoot//The purpose of this sequence traversal is to -{//combine the result array num with the process to get the CBT - if(root>n)return; -Inorder (root*2); +cbt[root]=number[shuzucount++]; -Inorder (root*2+1); + } A intMain () { atscanf"%d",&n); - for(intI=0; i<n;i++) - { -scanf"%d",&number[i]); - } -Sort (number,number+n); inInorder (1); - for(intI=1; i<=n;i++) to { +printf"%d", Cbt[i]); - if(i<n) printf (" "); the } * return 0; $}
A1064. Complete Binary Search Tree (30)