We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 17529b9 commit 64b235cCopy full SHA for 64b235c
4주차/박종운/레벨3__길_찾기_게임.js
@@ -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