Problem
You have n nodes labelled 0 to n - 1 and a list of undirected edges. Return true if these edges make a valid tree: every node is connected to every other, and there are no cycles.
Example 1
- Input
- n = 5, edges = [[0, 1], [0, 2], [0, 3], [1, 4]]
- Output
- true
Example 2
- Input
- n = 5, edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]
- Output
- false
- Why
- 1-2-3 is a cycle
Example 3
- Input
- n = 4, edges = [[0, 1], [2, 3]]
- Output
- false
- Why
- two pieces
Constraints
1 <= n <= 20000 <= edges.length <= 5000No self-loops and no repeated edges