Graph Valid Tree Problem
Graph Valid Tree Problem — ExecCode Medium DSA Practice
Solve the Graph Valid Tree problem on ExecCode. Free online medium DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given n nodes labeled from 0 to n - 1 and a list of undirected edges, determine whether these edges make up a valid tree. A valid tree is a connected acyclic undirected graph. Equivalently: the graph has exactly n - 1 edges and is fully connected (no cycles).
Examples
Input n = 6, edges = [[0, 1], [0, 2], [1, 3], [2, 4], [2, 5]]; Output true. Input n = 5, edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]; Output false. Input n = 4, edges = [[0, 1], [2, 3]]; Output false
Constraints
1 ≤ n ≤ 2000 0 ≤ edges.length ≤ 5000 edges[i].length == 2 0 ≤ ai, bi < n ai != bi There are no repeated edges.
Practice Graph Valid Tree free on ExecCode. Browse DSA problems, topic map, and placement guides.