Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Graph Valid Tree (medium)

Problem Statement

Given a number n, which indicates the number of nodes numbered from 0 to n-1, and a list of undirected edges for the graph, determine if the graph is a valid tree.

A graph qualifies as a valid tree if it meets the following criteria:

  1. It has no cycles.
  2. It is fully connected.


  • Example 1:

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

    • Justification: There are no cycles in the graph, and all nodes are connected, forming a valid tree.
  • Example 2:




Like the course? Get enrolled and start learning!