Graph Valid Tree
Beginner Mode

Problem Statement

You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that there is an undirected edge between nodes ai and bi in the graph.

Return true if the edges of the given graph make up a valid tree, and false otherwise.

A valid tree is a graph that meets two specific conditions:

  1. It is fully connected (all nodes can be reached from a single starting point).
  2. It contains absolutely no cycles.

Additional information

  • 1 <= n <= 2000
  • 0 <= edges.length <= 5000
  • edges[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • There are no self-loops or repeated edges.

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: The graph contains a cycle connecting nodes 1, 2, and 3.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →