[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
, andedges = [[0,1], [0,2], [0,3], [1,4]]
Output: true
Example 2:
Input:n = 5,
andedges = [[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
class Solution:
def validTree(self, n, edges):
if n <= 0: return False
d, queue, visited = collections.defaultdict(set), collections.deque([0]), set([0])
for n1, n2 in edges:
d[n1].add(n2)
d[n2].add(n1)
while queue:
node = queue.popleft()
for each in d[node]:
if each not in visited:
queue.append(node)
visited.add(node)
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
[LeetCode] 261. Graph Valid Tree _ Medium tag: BFS的更多相关文章
- [LeetCode] 261. Graph Valid Tree 图是否是树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode#261] Graph Valid Tree
Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
- [Locked] Graph Valid Tree
Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode Graph Valid Tree
原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- css笔记 - animation学习笔记(二)
animation动画 @keyframes规则 - 创建动画 from - to 等价于 0% - 100% 但是优先使用0% - 100%,因为浏览器兼容性还好点 animation 动画绑定 将 ...
- 常用linux命令及shell脚本
参考:Linux命令大全 分割大文件 Split命令 按行分割(只能是文本文件) $split -l 1000 big_file 前缀 按文件大小分割 $split -b 64m big_file 前 ...
- jQuery:find()方法与children()方法的区别
1:children及find方法都用是用来获得element的子elements的,两者都不会返回 text node,就像大多数的jQuery方法一样. 2:children方法获得的仅仅是元素一 ...
- jQuery相同id元素 全部获取问题解决办法
问题:今天在做页面链接的点击效果时,让部分a链接跳转到同一个地址,即使使用$().each()也同样无法获取所有相同id的值. 用以下方法只有第一个a链接点击可以正常跳转 例如: html代码: &l ...
- Twig---和vue或angular前端框架并存
<h1> {% verbatim %} {{message}} {% endverbatim %} </h1> 上面这种方式虽然能够解决,前台渲染的问题,但是还是会报错: 第二 ...
- PAT甲1077 Kuchiguse【字符串】【暴力】【Hash】【二分】
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...
- hihocoder 1330 - 数组重排 - [hiho一下167周][最小公倍数]
题目链接:https://hihocoder.com/problemset/problem/1330 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi想知道,如果他 ...
- Django的视图与网址之加法计算
在最新的Django2.1中,views.py中采用的地址映射方式发生了变化,通过一个加法运算我们来看一看. 方法一:在视图views.py中定义视图逻辑,求解两个数的加法运算:c = a + b,定 ...
- 各大互联网公司java开发面试常问问题
本人是做java开发的,这是我参加58,搜狐,搜狗,新浪微博,百度,腾讯文学,网易以及其他一些小的创业型公司的面试常被问的问题,当然有重复,弄清楚这些,相信面试会轻松许多. 1. junit用法,be ...
- 0003python中的可变参数
>>>def foo(x,y,z,*args,**kargs): print x print y print z print args print kargs >>> ...