LeetCode 261. Graph Valid Tree - 彻底掌握并查集(Union Find)系列题17

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = 5, edges = [[0,1],[1,2],[2,3],[1,3],[1,4]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值