Title: #261 - Graph Valid Tree - LeetCode JavaScript Solutions
Open Graph Title: #261 - Graph Valid Tree - LeetCode JavaScript Solutions
X Title: #261 - Graph Valid Tree - LeetCode JavaScript Solutions
Description: You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that...
Open Graph Description: You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that...
X Description: You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/graph-valid-tree
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/graph-valid-tree","name":"#261 - Graph Valid Tree - LeetCode JavaScript Solutions","description":"You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} n\n * @param {number[][]} edges\n * @return {boolean}\n */\nvar validTree = function(n, edges) {\n const parent = new Array(n).fill(-1);\n\n for (const [u, v] of edges) {\n if (!union(u, v)) return false;\n }\n\n let components = 0;\n for (let i = 0; i < n; i++) {\n if (parent[i] === -1) components++;\n if (components > 1) return false;\n }\n\n return edges.length === n - 1;\n\n function find(x) {\n if (parent[x] === -1) return x;\n return parent[x] = find(parent[x]);\n }\n\n function union(x, y) {\n const rootX = find(x);\n const rootY = find(y);\n if (rootX === rootY) return false;\n parent[rootX] = rootY;\n return true;\n }\n};","keywords":"LeetCode 261, #261 - Graph Valid Tree, 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/graph-valid-tree |
| 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/graph-valid-tree/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0261-graph-valid-tree.js |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Breadth-First Search | https://leetcodejavascript.com/tags/breadth-first-search |
| Graph | https://leetcodejavascript.com/tags/graph |
| Union Find | https://leetcodejavascript.com/tags/union-find |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow