- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1991.java
More file actions
Latest commit
76 lines (57 loc) · 2.05 KB
/
Copy path1991.java
File metadata and controls
76 lines (57 loc) · 2.05 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
sc.nextLine();
// 노드 리스트 생성 후 초기화
TreeNode[] nodeList = newTreeNode[n];
for(inti=0; i<n; i++)
nodeList[i] = newTreeNode((char) (i+'A'));
// 입력받아서 트리 저장
Stringline;
for(inti=0; i<n; i++) {
line = sc.nextLine();
if(line.charAt(2) != '.')
nodeList[line.charAt(0) - 'A'].left = nodeList[line.charAt(2) - 'A'];
if(line.charAt(4) != '.')
nodeList[line.charAt(0) - 'A'].right = nodeList[line.charAt(4) - 'A'];
}
// 순회하면서 출력
preOrder(nodeList, 0);
System.out.print('\n');
inOrder(nodeList, 0);
System.out.print('\n');
postOrder(nodeList, 0);
System.out.print('\n');
}
publicstaticvoidpreOrder(TreeNode[] nodeList, intidx) {
System.out.print(nodeList[idx].name);
if(nodeList[idx].left != null)
preOrder(nodeList, nodeList[idx].left.name - 'A');
if(nodeList[idx].right != null)
preOrder(nodeList, nodeList[idx].right.name - 'A');
}
publicstaticvoidinOrder(TreeNode[] nodeList, intidx) {
if(nodeList[idx].left != null)
inOrder(nodeList, nodeList[idx].left.name - 'A');
System.out.print(nodeList[idx].name);
if(nodeList[idx].right != null)
inOrder(nodeList, nodeList[idx].right.name - 'A');
}
publicstaticvoidpostOrder(TreeNode[] nodeList, intidx) {
if(nodeList[idx].left != null)
postOrder(nodeList, nodeList[idx].left.name - 'A');
if(nodeList[idx].right != null)
postOrder(nodeList, nodeList[idx].right.name - 'A');
System.out.print(nodeList[idx].name);
}
}
classTreeNode {
charname;
TreeNodeleft;
TreeNoderight;
TreeNode(charname) {
this.name = name;
}
}