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

 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的更多相关文章

  1. [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), ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. [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), ...

  8. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...

  9. 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), ...

随机推荐

  1. 【大数据系列】hadoop集群的配置

    一.hadoop的配置文件分类 1.只读类型的默认文件 core-default.xml     hdfs-default.xml    mapred-default.xml   mapred-que ...

  2. 移动端rem自适应布局(切图)

    本篇适用于初次使用rem为单位切图而无从下手的童鞋.核心是根据屏幕动态改变根元素字体大小,以达到适配各种屏幕.这只是一个拿来就用的教程.很多东西没有详细说明.不过对于快速做手机端切图很有帮助. 模板: ...

  3. linux下压缩和解压

    .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gun ...

  4. JVM学习--开启应用的gc日志功能

    一.开启方法 For Java 1.4, 5, 6, 7, 8 pass this JVM argument to your application: -XX:+PrintGCDetails -XX: ...

  5. 其它终端设备连接gmail账户提示密码错误解决方法

    换新手机配置Google Account继续使用Gmail服务,输入用户名.密码进入状态同步一段时间后再次提示输入用户名.密码并显示账号信息不正确.网上有人提到"修改用户密码"再进 ...

  6. The request associated with the AsyncContext has already completed processing

    Some time ago there was a problem with the servlet3.0, is in servlet in asynchronous processing data ...

  7. vi 撤销操作

    'u' : 撤销上一个编辑操作 'ctrl + r' : 恢复,即回退前一个命令 'U' : 行撤销,撤销所有在前一个编辑行上的操作

  8. Dockerfile ,ADD详细解读

    一.ADD指令 ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录.以及一个URL标记的文件 拷贝到镜像中. 其格式是: ADD  源路径  目标路径 如: #test FROM ubunt ...

  9. Unity3D笔记十五 碰撞、移动

    碰撞 Collision. [kəˈliʒən] 碰撞:冲突:(意见,看法)的抵触:(政党等的)倾轧 选择一个需要添加碰撞器的游戏对象后 Component->Physics- Unity一共为 ...

  10. mysql 存储过程用程序调的问题

    记一下,存储过程加了commit结果用mysql客户端执行不报错,用jdbcTemplate执行就执行了一步,因为加了commit后面的程序都不跑了.另外 存储过程里面如果用java调的话select ...