-
Notifications
You must be signed in to change notification settings - Fork 0
/
1068.js
48 lines (40 loc) · 1.21 KB
/
1068.js
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
/*
1. 일단 트리를 만든다
2. 연결관계를 끊는다
3. dfs로 리프노드 개수를 센다
*/
const makeTree = (parent) => {
let root;
const childs = Array.from(new Array(parent.length), () => []);
parent.forEach((parentNode, index) => {
if (parentNode === -1) {
root = index;
return;
}
childs[parentNode].push(index);
});
return { root, childs };
};
const countLeafNodesFrom = (start, childs) => {
if (childs[start].length === 0) return 1;
return childs[start].reduce(
(count, curNode) => count + countLeafNodesFrom(curNode, childs),
0,
);
};
const deleteNode = (target, parent, childList) => {
const childs = childList;
childs[parent] = childs[parent].filter((child) => child !== target);
return childs;
};
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [_, parent, [deletingNode]] = require('fs').readFileSync(INPUT_FILE).toString().trim()
.split('\n')
.map((line) => line.split(' ').map(Number));
const { root, childs } = makeTree(parent);
let leafCount = 0;
if (root !== deletingNode) {
deleteNode(deletingNode, parent[deletingNode], childs);
leafCount = countLeafNodesFrom(root, childs);
}
console.log(leafCount);