-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1991.java
More file actions
59 lines (50 loc) · 1.1 KB
/
P1991.java
File metadata and controls
59 lines (50 loc) · 1.1 KB
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
package BOJ;
import java.util.*;
public class P1991{
public static void preorder(int[][] tree,int x){
if(x==-1)
return;
System.out.print((char)(x+'A'));
preorder(tree,tree[x][0]);
preorder(tree,tree[x][1]);
}
public static void inorder(int[][] tree,int x){
if(x==-1)
return;
inorder(tree,tree[x][0]);
System.out.print((char)(x+'A'));
inorder(tree,tree[x][1]);
}
public static void postorder(int[][] tree,int x){
if(x==-1)
return;
postorder(tree,tree[x][0]);
postorder(tree,tree[x][1]);
System.out.print((char)(x+'A'));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] tree = new int[27][2];
sc.nextLine();
for(int i=0;i<n;i++){
String line = sc.nextLine();
int x = line.charAt(0)-'A';
char y = line.charAt(2);
char z = line.charAt(4);
if(y=='.')
tree[x][0] = -1;
else
tree[x][0] = y-'A';
if(z=='.')
tree[x][1] = -1;
else
tree[x][1] = z-'A';
}
preorder(tree,0);
System.out.println();
inorder(tree,0);
System.out.println();
postorder(tree,0);
}
}