René's URL Explorer Experiment


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

direct link

Domain: leetcodejavascript.com


Hey, it has json ld scripts:
{"@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"}

authorLeetCodeJavascript.com
og:typearticle
og:imagehttps://leetcodejavascript.com/og-image.jpg
og:site_nameLeetCode JavaScript Solutions
twitter:cardsummary_large_image
twitter:urlhttps://leetcodejavascript.com/solutions/tree-diameter
twitter:imagehttps://leetcodejavascript.com/og-image.jpg
twitter:creator@joshcrozier
theme-color#1f2937

Links:

LeetCodeJavascript.com https://leetcodejavascript.com/
Star on GitHubhttps://github.com/JoshCrozier/leetcode-javascript
Back to all solutionshttps://leetcodejavascript.com
View on LeetCode https://leetcode.com/problems/tree-diameter/
View on GitHubhttps://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 Sorthttps://leetcodejavascript.com/tags/topological-sort
Josh Crozierhttps://joshcrozier.com

Viewport: width=device-width,initial-scale=1

Robots: index, follow


URLs of crawlers that visited me.