-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0261-graph-valid-tree.js
More file actions
45 lines (39 loc) · 1.07 KB
/
0261-graph-valid-tree.js
File metadata and controls
45 lines (39 loc) · 1.07 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
/**
* 261. Graph Valid Tree
* https://leetcode.com/problems/graph-valid-tree/
* Difficulty: Medium
*
* 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 there is an undirected edge between nodes ai
* and bi in the graph.
*
* Return true if the edges of the given graph make up a valid tree, and false otherwise.
*/
/**
* @param {number} n
* @param {number[][]} edges
* @return {boolean}
*/
var validTree = function(n, edges) {
const parent = new Array(n).fill(-1);
for (const [u, v] of edges) {
if (!union(u, v)) return false;
}
let components = 0;
for (let i = 0; i < n; i++) {
if (parent[i] === -1) components++;
if (components > 1) return false;
}
return edges.length === n - 1;
function find(x) {
if (parent[x] === -1) return x;
return parent[x] = find(parent[x]);
}
function union(x, y) {
const rootX = find(x);
const rootY = find(y);
if (rootX === rootY) return false;
parent[rootX] = rootY;
return true;
}
};