1064. Graph Valid Tree

MediumGraphsDFSDSUGraph

Given n nodes labeled 0..n-1 and a list of undirected edges, determine whether the edges form a valid tree — the graph is fully connected and contains no cycle. Return true or false. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[0,3],[1,4]]}
Output: true
Explanation: Connected and acyclic.
Example 2
Input: {"n":5,"edges":[[0,1],[1,2],[2,3],[1,3],[1,4]]}
Output: false
Explanation: Contains a cycle.
Example 3
Input: {"n":1,"edges":[]}
Output: true
Explanation: A single node.

Constraints

Asked by

GoogleMetaMicrosoftAmazon
Solve this problem in the editor →