forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp218.java
73 lines (58 loc) · 1.92 KB
/
p218.java
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
import java.util.Scanner;
/**
* @author Rubén Saiz
*/
public class p218 {
static String[] inOrden;
static String[] postOrden;
static boolean space;
static void print(boolean[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
System.out.print((m[i][j]) ? "1 " : "0 ");
}
System.out.println();
}
}
static void printPreOrden(boolean[][] tree, int left, int right, int depht, int max) {
boolean exit = false;
for (int i = depht; i < max && !exit; i++) {
for (int j = left; j < right && !exit; j++) {
if (tree[i][j]) {
if (space) System.out.print(" ");
System.out.print(inOrden[j]);
space = true;
printPreOrden(tree, left, j, i + 1, max);
printPreOrden(tree, j + 1, right, i + 1, max);
exit = true;
}
}
}
}
public static void main(String[] args) {
final Scanner s = new Scanner(System.in);
int nodos;
boolean[][] tree;
while (true) {
nodos = s.nextInt();
if (nodos == 0) break;
s.nextLine();
tree = new boolean[nodos][nodos];
inOrden = s.nextLine().split(" ");
postOrden = s.nextLine().split(" ");
for (int i = nodos - 1; i >= 0; i--) {
boolean exit = false;
for (int j = 0; j < nodos && !exit; j++) {
if (postOrden[i].equals(inOrden[j])) {
tree[nodos - (i + 1)][j] = true;
exit = true;
}
}
}
// print(tree);
space = false;
printPreOrden(tree, 0, nodos, 0, nodos);
System.out.println();
}
}
}