http://www.patest.cn/contests/pat-a-practise/1099
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.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:9 1 6 2 3 -1 -1 -1 4 5 -1 -1 -1 7 -1 -1 8 -1 -1 73 45 11 58 82 25 67 38 42
Sample Output:58 25 82 11 38 67 45 73 42
此题是2015年春季的研究生入学考试复试时的机试题,链接 http://www.patest.cn/contests/graduate-entrance-exam-2015-03-20
这道题的考点有:树的构造、中序遍历(BST,递归)、层序遍历(层序输出,队列)、排序(怎么省事怎么来)。
可能看第一遍时觉得挺复杂的,但是如果树的遍历掌握的话,只要列出处理流程就很简单明了了。
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树):
它或者是一棵空树,或者是具有下列性质的二叉树:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树。
本题给出节点的左右孩子的下标和一些整型数据,要求构造出一个BST,然后层序输出这颗二叉树上所有节点的key值。
第一步,我们需要构造这棵BST,以便数据归位。实际上,这些节点本身就代表这棵二叉树,这一步使我们在头脑中(概念上)构造二叉树,不需要敲代码实现,考察的对二叉树的理解。
第二步,数据是无序的,所以为了数据归位,需要进行排序。数据量很小,不超过100,所以怎么简单怎么排,反正内存大小和时间足足的.
第三步,进行数据归位。根据bst的性质,其实就是中序处理每个节点。
第四步,按要求层序输出即可。
总结:输入->keys排序->中序遍历,key值归位->层序遍历,格式输出。
1 #include<cstdio> 2 #include<cstring> 3 // a positive integer N (<=100) 4 int num=0,node[100][3]={{0,0,0}},keys[100]={0},keyloc=0; 5 void levelprint() 6 { 7 int level[100]={0},start=0,len=0; 8 level[start]=0,len++; 9 while(len) 10 { 11 if(node[level[start]][1]!=-1)//左子树 12 level[start+len]=node[level[start]][1],len++; 13 if(node[level[start]][2]!=-1)//左子树 14 level[start+len]=node[level[start]][2],len++; 15 16 if(start) printf(" "); 17 printf("%d",node[level[start]][0]); 18 start++,len--; 19 } 20 21 } 22 void inorder(int rootloc) 23 { 24 if(node[rootloc][1]!=-1)//左子树 25 inorder(node[rootloc][1]); 26 node[rootloc][0]=keys[keyloc],keyloc++; //本节点 27 // printf(" %d %d %d %d",rootloc,node[rootloc][0],node[rootloc][1],node[rootloc][2]); 28 if(node[rootloc][2]!=-1)//右子树 29 inorder(node[rootloc][2]); 30 } 31 void keyssort() 32 { 33 for(int i=1;i<num;i++) 34 { 35 int key=keys[i],loc=i-1; 36 for(;loc>=0;loc--) if(keys[loc]<=key) break; 37 loc++; 38 for(int j=i;j>loc;j--) keys[j]=keys[j-1]; 39 keys[loc]=key; 40 } 41 } 42 int main() 43 { 44 scanf("%d",&num); 45 for(int i=0;i<num;i++) scanf("%d%d",&node[i][1],&node[i][2]); 46 for(int i=0;i<num;i++) scanf("%d",keys+i); 47 keyssort();//keys排序 升序 48 inorder(0);//中序遍历,keys归位 49 levelprint();//层序输出 50 return 0; 51 } 52