You are given n nodes labeled 0 to n - 1 and a list of undirected edges. Return true if the edges form a valid tree: connected, and with no cycle. A single node with no edges is a tree.
Examples
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
Explanation: 1-2-3 closes a cycle.
Example 3:
Input: n = 4, edges = [[0,1],[2,3]]
Output: false
Explanation: acyclic but disconnected — a forest, not a tree.
Example 4:
Input: n = 1, edges = []
Output: true
Constraints
1 <= n <= 2000
0 <= edges.length <= 5000
No self-loops or duplicate edges.
Prerequisites
Graph traversal — DFS and BFS, and a visited set as the thing that makes both terminate. Union-find for connectivity questions; topological order for dependency ones.
How to think about it
1. Join Each Edge's Two Roots Optimal
Intuition
A tree on n nodes has exactly n - 1 edges: with fewer, some node is left out, and with more, there is a loop. So count first. Exactly n - 1 edges can still close a loop, and then a loop also leaves the graph in pieces, so join the edges one at a time and watch for one. Every node starts as a group of its own, its own root. For an edge, find the root of each end: two different roots are two groups, and pointing one root at the other makes them one; the same root means the two ends are already joined, so this edge would close a loop. If every edge joins two groups, there is no loop, and n - 1 joins leave one group: a tree.
Algorithm
1. Without exactly n - 1 edges, return false. 2. Start parent with every node as its own root. 3. For each edge, find the root of each end, rootA and rootB. 4. If they are the same, the edge closes a loop: return false. Otherwise point rootA at rootB. 5. Every edge joined two groups: return true. 6. find(x) climbs while x is not its own root, pointing x at its grandparent on each step, and returns the root.
Time & Space
Time O(n log n) at most: thanks to the count, at most n - 1 edges reach the loop, each makes two finds, and with the halving all the climbs together take O(n log n) at most. Space O(n): parent holds an entry for each node.
2. Count the Edges, Then Check Connectivity Optimal
Intuition
A tree on n nodes has exactly n-1 edges — that single check does half the work, because fewer means disconnected and more guarantees a cycle. With the count right, connectivity is the only remaining question, and connected plus n-1 edges IMPLIES acyclic. Union-find answers both in one pass: a union that finds both nodes already joined is a cycle.
Algorithm
1. Reject immediately unless there are exactly n-1 edges. 2. Union each edge; an edge whose endpoints already share a root closes a cycle. 3. Everything ending in one component is a tree.
Time & Space
Time near O(n + edges) with path compression. Space O(n).