[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), ...
随机推荐
- Ubuntu 14.04 LTS 火狐浏览器中,鼠标选择文字被删除的解决办法
这篇文章主要介绍了Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法,需要的朋友可以参考下在终端中输入命令: ibus-setup将 “在应用程序窗口中启用内嵌编辑模式“ 选项取消
- 邮件MIME格式分析
http://www.cnblogs.com/crystalray/articles/3302427.html 邮件mime格式 参考: rfc4021,Registration of Mail an ...
- storm并发度理解
1. 核心原理 一个运行中的拓扑是由什么组成的:worker进程,executors和tasks.Storm是按照下面3种主要的部分来区分Storm集群中一个实际运行的拓扑的:Worker进程.Exe ...
- sencha touch 开发环境搭建(视频)
图文文章参见: http://www.cnblogs.com/mlzs/p/3420900.html 视频共享链接 百度:http://pan.baidu.com/s/1mg5DpS8
- 2-5 vue基础语法
一.vue基础语法 语法: {{msg}} html赋值: v-html="" 绑定属性: v-bind:id="" 使用表达式: {{ok? "ye ...
- dhroid - NetJSONAdapter 网络化的adapter
关于adapter 我想对于大家来说已经不陌生了,基本应用都会用的很多,不知道现在你是不是还是按一定的套路写很多代码去实现adapter我想大多数人还是写个adapter继承自baseadapter ...
- dubbo入门之helloWorld
dubbo官方文档:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html 基于spring coloud的demo:http://start ...
- Spring JPA配置讲解
JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 Spring提供三种方法集成JPA: 1. ...
- python3中如何区分一个函数和方法
一般情况下,单独写一个def func():表示一个函数,如果写在类里面是一个方法.但是不完全准确. class Foo(object): def fetch(self): pass print(Fo ...
- pyAudio介绍
概要 pyaudio有这么几个功能: - 提取特征 - 训练并且使用分类器 - 语音分割功能 - 内容关系可视化 python实现,好处有这么几个 - 适合做计算分析类型操作(编码少,效率不低) - ...