0261. Graph Valid Tree
https://leetcode.com/problems/graph-valid-tree
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 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.
Example 1:

Example 2:

Constraints:
1 <= 2000 <= n0 <= edges.length <= 5000edges[i].length == 20 <= ai, bi < nai != biThere are no self-loops or repeated edges.
ac1: Union-find
UF template
Valid Tree: 1) all connected, 2)edges + 1 = vertices, 3)no cycle. 1)&2) is easier, but still need to know how to detect cycle.
ac2: Graph + DFS
Last updated
Was this helpful?