-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1245-tree-diameter.js
More file actions
60 lines (51 loc) · 1.46 KB
/
1245-tree-diameter.js
File metadata and controls
60 lines (51 loc) · 1.46 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
/**
* 1245. Tree Diameter
* https://leetcode.com/problems/tree-diameter/
* Difficulty: Medium
*
* The diameter of a tree is the number of edges in the longest path in that tree.
*
* There is an undirected tree of n nodes labeled from 0 to n - 1. You are given a 2D array
* edges where edges.length == n - 1 and edges[i] = [ai, bi] indicates that there is an
* undirected edge between nodes ai and bi in the tree.
*
* Return the diameter of the tree.
*/
/**
* @param {number[][]} edges
* @return {number}
*/
var treeDiameter = function(edges) {
if (edges.length === 0) return 0;
const graph = new Map();
for (const [a, b] of edges) {
if (!graph.has(a)) graph.set(a, []);
if (!graph.has(b)) graph.set(b, []);
graph.get(a).push(b);
graph.get(b).push(a);
}
const [farthestFromStart] = bfs(0);
const [, diameter] = bfs(farthestFromStart);
return diameter;
function bfs(start) {
const visited = new Set();
const queue = [[start, 0]];
visited.add(start);
let farthestNode = start;
let maxDistance = 0;
while (queue.length > 0) {
const [node, distance] = queue.shift();
if (distance > maxDistance) {
maxDistance = distance;
farthestNode = node;
}
for (const neighbor of graph.get(node) || []) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push([neighbor, distance + 1]);
}
}
}
return [farthestNode, maxDistance];
}
};