Title: #1245 - Tree Diameter - LeetCode JavaScript Solutions
Open Graph Title: #1245 - Tree Diameter - LeetCode JavaScript Solutions
X Title: #1245 - Tree Diameter - LeetCode JavaScript Solutions
Description: 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 -...
Open Graph Description: 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 -...
X Description: 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 -...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/tree-diameter
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/tree-diameter","name":"#1245 - Tree Diameter - LeetCode JavaScript Solutions","description":"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 -...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[][]} edges\n * @return {number}\n */\nvar treeDiameter = function(edges) {\n if (edges.length === 0) return 0;\n\n const graph = new Map();\n for (const [a, b] of edges) {\n if (!graph.has(a)) graph.set(a, []);\n if (!graph.has(b)) graph.set(b, []);\n graph.get(a).push(b);\n graph.get(b).push(a);\n }\n\n const [farthestFromStart] = bfs(0);\n const [, diameter] = bfs(farthestFromStart);\n\n return diameter;\n\n function bfs(start) {\n const visited = new Set();\n const queue = [[start, 0]];\n visited.add(start);\n let farthestNode = start;\n let maxDistance = 0;\n\n while (queue.length > 0) {\n const [node, distance] = queue.shift();\n\n if (distance > maxDistance) {\n maxDistance = distance;\n farthestNode = node;\n }\n\n for (const neighbor of graph.get(node) || []) {\n if (!visited.has(neighbor)) {\n visited.add(neighbor);\n queue.push([neighbor, distance + 1]);\n }\n }\n }\n\n return [farthestNode, maxDistance];\n }\n};","keywords":"LeetCode 1245, #1245 - Tree Diameter, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/tree-diameter |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/tree-diameter/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1245-tree-diameter.js |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Tree | https://leetcodejavascript.com/tags/tree |
| Breadth-First Search | https://leetcodejavascript.com/tags/breadth-first-search |
| Graph | https://leetcodejavascript.com/tags/graph |
| Topological Sort | https://leetcodejavascript.com/tags/topological-sort |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow