[LeetCode] 261. Graph Valid Tree _ Medium tag: BFS

Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

Example 1:

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

Example 2:

Input: n = 5, and edges = [[0,1], [1,2], [2,3], [1,3], [1,4]]
Output: false

Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0,1] is the same as [1,0] and thus will not appear together in edges.

 

这个题的思路就是BFS, 另外注意有效的tree的条件是nodes number == number of edges + 1 and connects all nodes, 所以首先用这个条件去掉所有点连通并且有loop的情况, 所以还有两种情况, 一种是valid tree, 另一种是有重复的loop, 但是不连通, 所以我们用BFS选择任一点开始, 我们选择0, 因为所有情况都包括0这个node, 然后BFS, 最后把visited set的size跟input n比较是否相等, 如果相等, valid, 否则不valid.

 

1. Constraints

1) n <=0 时, False

2) n =1是, edge = []  =>  True

3) no duplicates and (1,0) and (0,1) will not exist at same time, 这个条件保证了nodes number == number of edges + 1 的判断依据

 

2. Ideas

BFS     T: O(n)     S: O(n)

1) edge case, n <=0 时, False

2) transform input into a dictionary

3) start from 0 , BFS, save all visited nodes into a visited set

4) return len(visited) == n

 

3. Code

 1 class Solution:
 2     def validTree(self, n, edges):
 3         if n <= 0: return False
 4         d, queue, visited = collections.defaultdict(set), collections.deque([0]), set([0])
 5         for n1, n2 in edges:
 6             d[n1].add(n2)
 7             d[n2].add(n1)
 8         while queue:
 9             node = queue.popleft()
10             for each in d[node]:
11                 if each not in visited:
12                     queue.append(node)
13                     visited.add(node)
14         return len(visited) == n

 

4. Test cases

1)  1, []  => True

2) 3, [[0,1], [0,2]]    => True

3) 5, [[0,1], [3, 4], [3,5], [4,5]]  =>   False

转载于:https://www.cnblogs.com/Johnsonxiong/p/9271665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值