We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8588d76 commit 145b380Copy full SHA for 145b380
3주차/김유경/레벨3__가장_먼_노드.js
@@ -0,0 +1,30 @@
1
+function solution(n, edge) {
2
+ const graph = Array.from(Array(n + 1), () => []);
3
+ let distance = Array(n + 1).fill(0);
4
+
5
+ edge.forEach(([u, v]) => {
6
+ graph[u].push(v);
7
+ graph[v].push(u);
8
+ });
9
10
+ bfs(1);
11
12
+ function bfs(i) {
13
+ const queue = [i];
14
+ distance[i] = 1;
15
16
+ while (queue.length) {
17
+ const x = queue.shift();
18
19
+ graph[x].forEach((e) => {
20
+ if (distance[e] === 0) {
21
+ distance[e] = distance[x] + 1;
22
+ queue.push(e);
23
+ }
24
25
26
27
+ const max = Math.max(...distance);
28
29
+ return distance.filter((e) => e === max).length;
30
+}
0 commit comments