LeetCode 261 Graph Valid Tree - Problem Statement

The problem

Given n nodes labeled 0 to n-1 and a list of undirected edges, determine if these edges form a valid tree.

A valid tree has exactly n - 1 edges, is connected, and has no cycles.

With n = 5 and edges = [[0,1],[0,2],[0,3],[1,4]]:

  • 5 nodes, 4 edges (n-1). ✓
  • All nodes reachable from any node. ✓
  • No cycles. ✓
  • Valid tree. Return true.

With n = 5 and edges = [[0,1],[1,2],[2,3],[1,3],[1,4]]:

  • 5 edges > 4. Has a cycle (1-2-3-1). Return false.

Constraints: 1n20001 \le n \le 2000.