Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Graph Valid Tree

Problem in Plain English

Given a node count and undirected edges, decide whether they form exactly one tree. A valid tree connects every node with no cycles.

Problem Statement

You are given n nodes labeled 0 to n - 1 and an undirected edges list.

Return true when the edges form a valid tree: every node reachable from every other node, with no cycles. Any connected graph with exactly n - 1 edges qualifies.

Constraints

  • 1≤n≤20001 \le n \le 2000
  • 0≤edges.length≤50000 \le \text{edges.length} \le 5000
  • Each edge has exactly two node labels.
  • 0≤a,b<n0 \le a,b < n
  • No duplicate edges.

Examples

Example 1

Input
n = 5, edges = [[0,1],[0,2],[0,3],[1,4]]
Output
true

Four edges link all five nodes with no cycle, which is exactly a tree.

Example 2

Input
n = 5, edges = [[0,1],[1,2],[2,3],[1,3],[1,4]]
Output
false

Edges 1-2, 2-3 and 1-3 close a cycle, so the graph cannot be a tree.

Example 3

Input
n = 1, edges = []
Output
true

The single node is connected to itself by the empty path. It needs no edges and contains no cycle.

Example 4

Input
n = 4, edges = [[0,1],[1,2],[2,0]]
Output
false

The edge count is n - 1, but nodes 0, 1 and 2 form a cycle and node 3 is isolated. Counting edges alone would accept this invalid graph.

Example 5

Input
n = 4, edges = [[0,1],[2,3]]
Output
false

Both components are individually acyclic, but there is no path from 0 to 2. Two edges are fewer than the three needed to connect all four nodes.

Intuition

A tree uses one edge to attach each new node to the nodes already connected, so it needs exactly n - 1 edges and cannot join two nodes already linked by a path. In Example 1, 0 attaches 1, 2 and 3, then 1 attaches 4. The edge count alone is insufficient: a triangle plus an isolated node has three edges on four nodes but is not connected. DFS checks reachability and cycles directly; union-find remembers connected groups without constructing adjacency lists.

Approaches

DFS Connectivity

Solution Details

Reveal DFS Connectivity: intuition, complexity, and code

Hints

Hint 1
Count first: a tree on n nodes always has exactly n - 1 edges, so any other count answers false at once.
Hint 2
For the rest, test both halves: no cycles plus every node reachable means a tree.
Hint 3
With union-find, an edge joining two nodes of the same group is the cycle, and n - 1 successful merges prove connection.

Edge Cases

  • One node with no edges returns true; no special rejection of an empty edge list is needed.
  • A triangle on nodes 0, 1 and 2 with node 3 isolated has n - 1 edges but returns false. A disconnected forest such as [[0,1],[2,3]] also fails, this time already at the count check.
  • A 2000-node path is a valid tree; explicit DFS frames avoid Python's default recursion depth limit. A star of the same size tests a wide adjacency list.
  • If a self-loop is supplied, it cannot belong to a tree: it either fails the count or hits the non-parent revisit / equal-root test. Duplicate edges are excluded by the input contract; the DFS parent shortcut relies on that contract.

Common Mistakes and Interview Tips

  • Accepting n - 1 edges without a structural test; Example 4 has that count but contains a triangle and an isolated node.
  • Treating the reverse undirected edge as a cycle; skip the parent, while still rejecting other visited neighbors.
  • Using recursive Python DFS on a legal 2000-node chain without accounting for the interpreter's recursion limit. The explicit-frame version preserves DFS without changing interpreter settings.
  • Unioning raw parent values rather than roots, or incrementing rank on every merge. Rank changes only when equal-rank roots are linked.
  • Interview tip: prove that each successful union reduces the number of components by exactly one. That explains why a separate final connectivity check is unnecessary after n - 1 successful unions.

Key Takeaway

Tree validation splits into counting edges and testing structure; union-find folds both into one pass by treating a redundant union as a cycle.