pta 7-7 Compplete Binary Search Tree(30分)
8909 ワード
7-7 Complete Binary Search Tree(30分)
A Binary Search Tree(BST)recucursively defined a s a binary tree which has the follwing properties:The left subtree of a node contains only node s with key s less ththththaaathe node’s key.The rightsubtreeof a node containininininstststststrererererererererererereaaaaaatototototorererererererererererererererererererererererererererererererererererereaaaaaaaaaaaaaaatotototototototototototorererererererererereretree s.A Complete Binary Tree(CBT)is a tree that is complettely 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 out put the level order trquence of this BST.
Input specification:
Each input file contains one test case.For each case,the first line contains a positive integer N(≦1000).The n N distinct non-negative integer keys are given in the next line.All the numbers a inter a line e e e e.sprate.spree e.paree e.paree.paree.paree.paree.paree.paree.paree.paree.paree.All.paree.
Output Specification:
For each test case,print in one line the level order trversal sequence of the cores ponding corese binary search tre.All the numbers in a line must be separated bya space,and the muse must no extra space the space the the end line.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.
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
前順序を利用した思想.前から順番に見ていくと、小さいころから大将の数字に二叉の木を入れると、ちょうど二叉の捜索樹ができます.
A Binary Search Tree(BST)recucursively defined a s a binary tree which has the follwing properties:The left subtree of a node contains only node s with key s less ththththaaathe node’s key.The rightsubtreeof a node containininininstststststrererererererererererereaaaaaatototototorererererererererererererererererererererererererererererererererererereaaaaaaaaaaaaaaatotototototototototototorererererererererereretree s.A Complete Binary Tree(CBT)is a tree that is complettely 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 out put the level order trquence of this BST.
Input specification:
Each input file contains one test case.For each case,the first line contains a positive integer N(≦1000).The n N distinct non-negative integer keys are given in the next line.All the numbers a inter a line e e e e.sprate.spree e.paree e.paree.paree.paree.paree.paree.paree.paree.paree.paree.All.paree.
Output Specification:
For each test case,print in one line the level order trversal sequence of the cores ponding corese binary search tre.All the numbers in a line must be separated bya space,and the muse must no extra space the space the the end line.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.ine.
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
前順序を利用した思想.前から順番に見ていくと、小さいころから大将の数字に二叉の木を入れると、ちょうど二叉の捜索樹ができます.
#include
#include
#include
int n,i,l1[1001],l2[2010];
int c=0;
void inorder(int k)
{
if(k>n)
return;
inorder(k*2);
l2[k]=l1[c++];
inorder(k*2+1);
}
void levelprint()
{
for(i=1;i<=n;i++)
{
if(i==1)
printf("%d",l2[i]);
else
printf(" %d",l2[i]);
}
}
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&l1[i]);
qsort(l1,n,sizeof(l1[0]),cmp);
inorder(1);
levelprint();
}