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.
Example 1:

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

Input:</

给定一个节点数为n的无向图,你需要判断其边是否构成一棵有效的树。有效树需满足两个条件:1) 图中没有环;2) 所有顶点都是直接或间接连接的。通过使用并查集(Union Find)算法,当合并边的两个顶点已经在同一集合中时发现环,或者最后并查集中集合数量大于1,则该图不是有效树。
最低0.47元/天 解锁文章
441

被折叠的 条评论
为什么被折叠?



