Skip to content

Commit 64b235c

Browse files
committed
Feat: 레벨3 길 찾기 게임
1 parent 17529b9 commit 64b235c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: 4주차/박종운/레벨3__길_찾기_게임.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution(nodeinfo) {
2+
const nodes = nodeinfo.map((el, i) => [i+1, ...el]);
3+
4+
const sorted = nodes.sort((a, b) => b[2] === a[2] ? a[1] - b[1] : b[2] - a[2]);
5+
6+
const preorder = [];
7+
const postorder = [];
8+
9+
function traverse(arr) {
10+
if(!arr.length) return;
11+
12+
const current = arr[0];
13+
14+
preorder.push(current[0]);
15+
16+
const newLeft = arr.filter(el => el[2] < current[2] && el[1] < current[1]);
17+
const newRight = arr.filter(el => el[2] < current[2] && el[1] > current[1] );
18+
19+
traverse(newLeft);
20+
traverse(newRight);
21+
22+
postorder.push(current[0]);
23+
}
24+
25+
traverse(sorted);
26+
27+
return [preorder, postorder];
28+
}

0 commit comments

Comments
 (0)