-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1466.c
100 lines (81 loc) · 2.13 KB
/
1466.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
//1466 - Percurso em Árvore por Nível
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_TAM 1000
struct regNo { struct regNo *esq;
int num;
struct regNo *dir;
};
typedef struct regNo TArvore;
TArvore * FuncPai(TArvore *r, int n)
{ if(r == NULL)
return NULL;
else if (n == r->num){
return r;
}
else if(n < r->num)
{ if(r->esq == NULL)
return r;
else
return FuncPai(r->esq, n);
}
else
{
if(r->dir == NULL)
return r;
else
return FuncPai(r->dir, n);
}
}
int main()
{ TArvore *raiz, *pai, *aux, *fila[MAX_TAM];
int nVal, Ncasos, nC, cont, noh, contA, contB;
scanf("%d", &Ncasos);
for(nC=1; nC<=Ncasos; nC++)
{
for(cont=0;cont<MAX_TAM;cont++)
fila[cont] = NULL;
raiz = NULL;
scanf("%d", &nVal);
for(cont=0; cont<nVal; cont++)
{
scanf("%d", &noh);
aux = (TArvore *) malloc(sizeof(TArvore));
aux->num = noh;
aux->esq = aux->dir = NULL;
pai = FuncPai(raiz, noh);
if(pai == NULL)
raiz = aux;
else if (pai->num != noh){
if (noh < pai->num)
pai->esq = aux;
else
pai->dir = aux;
}
}
printf("Case %d:\n", nC);
aux = raiz;
contA = 1;
contB = 1;
fila[0] = aux;
while(aux != NULL){
printf("%d", aux->num);
if(aux->esq != NULL){
fila[contA] = aux->esq;
contA++;
}
if(aux->dir != NULL){
fila[contA] = aux->dir;
contA++;
}
if(fila[contB]!=NULL)
printf(" ");
aux = fila[contB];
contB++;
}
printf("\n\n");
}
return 0;
}