Skip to content

Commit 8476965

Browse files
committed
코드 수정
1 parent 860d20d commit 8476965

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/땃쥐/BOJ_1068.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,48 @@
77

88
public class BOJ_1068 {
99

10-
private static int N; // 노드의 갯수
10+
private static int N;
1111
private static Node[] nodes;
1212

1313
public static void main(String[] args) throws IOException {
14-
N = readInt();
15-
16-
nodes = new Node[N];
14+
initNodes();
15+
setParents();
16+
detachNode();
17+
int numberOfLeafNodes = numberOfLeafNodes();
18+
System.out.print(numberOfLeafNodes);
19+
}
1720

18-
for (int i = 0; i < N; i++) {
19-
nodes[i] = new Node(i);
20-
}
21+
private static int numberOfLeafNodes() {
22+
int count = 0;
2123

22-
for (int i = 0; i < N; i++) {
23-
int parentNodeNumber = readInt();
24-
if (isNotRootNode(parentNodeNumber)) {
25-
nodes[parentNodeNumber].addChild(nodes[i]);
24+
for (Node node : nodes) {
25+
if (node.isLeafNode()) {
26+
count++;
2627
}
2728
}
29+
return count;
30+
}
2831

32+
private static void detachNode() throws IOException {
2933
int deleteNodeNumber = readInt();
3034
nodes[deleteNodeNumber].detach();
35+
}
3136

32-
int count = 0;
33-
34-
for (Node node : nodes) {
35-
if (node.isLeafNode()) {
36-
count++;
37+
private static void setParents() throws IOException {
38+
for (int i = 0; i < N; i++) {
39+
int parentNodeNumber = readInt();
40+
if (parentNodeNumber >= 0) {
41+
nodes[i].changeParent(nodes[parentNodeNumber]);
3742
}
3843
}
39-
40-
System.out.print(count);
4144
}
4245

43-
private static boolean isNotRootNode(int parentNodeNumber) {
44-
return parentNodeNumber != -1;
46+
private static void initNodes() throws IOException {
47+
N = readInt();
48+
nodes = new Node[N];
49+
for (int i = 0; i < N; i++) {
50+
nodes[i] = new Node(i);
51+
}
4552
}
4653

4754
private static int readInt() throws IOException {
@@ -77,9 +84,9 @@ public int getValue() {
7784
return value;
7885
}
7986

80-
public void addChild(Node child) { // 양방향 참조
81-
childs.add(child);
82-
child.parent = this;
87+
public void changeParent(Node parent) { // 양방향 참조
88+
this.parent = parent;
89+
parent.childs.add(this);
8390
}
8491

8592
public Node getRootNode() {

0 commit comments

Comments
 (0)