Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

n =
edges =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Graph Valid Tree

Medium

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

Return true if the edges form a valid tree, and false otherwise.

A valid tree is an undirected graph that is connected and contains no cycles. Equivalently, for n nodes it must have exactly n - 1 edges and be connected.

Example:

Input:
5 [[0,1],[0,2],[0,3],[1,4]]
Output:
true

Constraints:

  • 1 <= n <= 2000
  • 0 <= edges.length <= 10^4
  • edges[i] = [u, v] with 0 <= u, v < n

Graph Valid Tree

Medium

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

Return true if the edges form a valid tree, and false otherwise.

A valid tree is an undirected graph that is connected and contains no cycles. Equivalently, for n nodes it must have exactly n - 1 edges and be connected.

Example:

Input:
5 [[0,1],[0,2],[0,3],[1,4]]
Output:
true

Constraints:

  • 1 <= n <= 2000
  • 0 <= edges.length <= 10^4
  • edges[i] = [u, v] with 0 <= u, v < n