Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-“ will be put at the position. Any pair of children are separated by a space.

Output Specification

For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input

1
2
3
4
5
6
7
8
9
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output

1
4 1 5

代码实现

C语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<stdio.h>

#define MaxTree 10
#define MaxQueue 10
#define ElementType int
#define Tree int
#define Null -1

struct TreeNode {
Tree Left;
Tree Right;
} T[MaxTree];

struct QNode { // 结构中有Roots队列
Tree Roots[MaxQueue];
int rear;
int front;
} Q;

Tree BuildTree(struct TreeNode T[]) { // 建立结构的数组,每个数组下标表示节点编号,每个节点储存节点编号,左右节点编号
Tree Root = Null;
char cl, cr;
int N;
scanf("%d", &N);
int check[N];
for ( int i=0; i<N; i++ ) {
check[i] = 0;
}
if ( N ) {
for ( int i=0; i<N; i++ ) {
getchar();
scanf(" %c %c", &cl, &cr);
if ( cl != '-') {
T[i].Left = cl - '0';
check[T[i].Left] = 1;
} else {
T[i].Left = Null;
}
if ( cr != '-' ) {
T[i].Right = cr - '0';
check[T[i].Right] = 1;
} else {
T[i].Right = Null;
}
}
for ( int i=0; i<N; i++ ) {
if ( !check[i] ) {
Root = i;
break;
}
}
}
return Root;
}

/*
void PreOrderTraversal(Tree Root) { // 树的前中后序遍历,本题用不到
if ( Root != Null ) {
if ( T[Root].Left==Null && T[Root].Right==Null ) {
printf("%d ", Root);
}
PreOrderTraversal(T[Root].Left);
PreOrderTraversal(T[Root].Right);

}
}
*/

void AddQ(Tree Root) { // 加入队列
if ( (Q.rear+1) % MaxQueue == Q.front ) {
printf("The queue is full\n");
} else {
Q.rear = (Q.rear+1) % MaxQueue;
Q.Roots[Q.rear] = Root;
}
}

Tree DeleteQ(Tree Root) { // 取出队列
if ( Q.front == Q.rear ) {
printf("The queue is empty\n");
return Null;
} else {
Q.front = (Q.front+1) % MaxQueue;
return Q.Roots[Q.front];
}
}

int IsEmptyQ() { // 判断队列是否为空
int check = 0;
if ( Q.front == Q.rear ) {
check = 1;
}
return check;
}

void LevelOrderTraversal(Tree Root) { // 树的层序遍历
AddQ(Root);
int flag = 0;
while ( !IsEmptyQ() ) {
Root = DeleteQ(Root);
if ( T[Root].Left == Null && T[Root].Right == Null ) { // 只输出叶节点
if ( flag ) {
printf(" %d", Root);
} else {
printf("%d", Root);
flag = 1;
}
}
if ( T[Root].Left != Null ) AddQ(T[Root].Left);
if ( T[Root].Right != Null ) AddQ(T[Root].Right);
}
}

int main() {
Tree R;
R = BuildTree(T); // 建立节点构成的数组,数组下标表示节点编号,节点内储存左右节点编号。
LevelOrderTraversal(R); // 层序遍历,按从上到下从左到右输出,只输出叶节点。
return 0;
}