您的位置:首页 > 产品设计 > UI/UE

pat 1099. Build A Binary Search Tree (30)

2015-10-15 15:20 399 查看


1099. Build A Binary Search Tree (30)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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


给定一颗二叉树,和树上的数字,但不知道和树上每个节点的对应关系。

这里用二维数组来存储树,tree[i][0]存的是i节点的左儿子,tree[i][1]存的是i节点的右儿子.

思路是因为给定的N个数是各不相同的,并且通过二叉树可以对N个数进行从小到大的排序也就是通过中序排序(这里通过深度遍历的方式所以命名为deep_sort)的方法,得到对应节点的大小关系,再将N个数从小到大排序,就得到每个节点上的对应数字是什么。如,例子中通过中序遍历的节点序列是:2 1 3 6 4 0 7 8 6,

n个数排序后得到 11 25 38 42 45 58 67 73 82.因此知道每个节点对应数字如题中图2 所示。

题目要求给出层序遍历的数字序列,所以应用数组模拟队列来对数字进行层序遍历。得到对应的程序遍历的节点编号序列。最后输出对应的数字序列即可。

具体代码如下:

#include<stdio.h>
#include<algorithm>
using namespace std;
int num[110],treen[110],deep_num[110],level[110];
int tree[110][2];
int deepsort(int root,int first){//root是根节点
int l,r,now=first;
l=tree[root][0];
r=tree[root][1];

if(l!=-1){
now=deepsort(l,first);//遍历左子树
}
deep_num[now]=root;//放入根节点
now++;
if(r!=-1){
now=deepsort(r,now);//遍历右子树
}

return now;
}

void level_sort(int n){
int i,j;
int l=0,now=0,nnow;
level[0]=0;
while(l<n&&now<n){
nnow=level[now];
if(tree[nnow][0]!=-1)
level[++l]=tree[nnow][0];
if(tree[nnow][1]!=-1)
level[++l]=tree[nnow][1];

now++;
}
}

int main(){
int n;
int i,j,l,r;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d%d",&l,&r);
tree[i][0]=l;
tree[i][1]=r;

}

for(i=0;i<n;i++)
scanf("%d",&num[i]);
sort(num,num+n);
deepsort(0,0);
level_sort(n);
for(i=0;i<n;i++){
treen[deep_num[i]]=num[i];
}
for(i=0;i<n-1;i++){
printf("%d ",treen[level[i]]);
}
printf("%d\n",treen[level[n-1]]);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  代码 搜索 遍历