# [Swift]LeetCode261.图验证树 \$ 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), write a function to check whether these edges make up a valid tree.

For example:

Given `n = 5` and `edges = [[0, 1], [0, 2], [0, 3], [1, 4]]`, return `true`.

Given `n = 5` and `edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]`, return `false`.

Hint:

1. Given `n = 5` and `edges = [[0, 1], [1, 2], [3, 4]]`, what should your return? Is this case a valid tree?
2. According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”

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

``` 1 class Solution {
2     func validTree(_ n:Int,_ edges:[[Int]]) -> Bool{
3         var g:[Set<Int>] = [Set<Int>](repeating:Set<Int>(),count:n)
4         var s:Set<Int> = [0]
5         var q:[Int] = [0]
6         for a in edges
7         {
8             g[a[0]].insert(a[1]);
9             g[a[1]].insert(a[0]);
10         }
11         while(!q.isEmpty)
12         {
13             var t:Int = q.first!
14             q.removeFirst()
15             for a in g[t]
16             {
17                 if s.contains(a)
18                 {
19                     return false
20                 }
21                 s.insert(a)
22                 q.append(a)
23                 g[a].remove(t)
24             }
25         }
26          return s.count == n
27     }
28 }```

## Leetcode: Graph Valid Tree &amp;&amp; Summary: Detect cycle in directed graph and undirected graph

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. For example: Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return tru

## [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 of nodes), write a function to check whether these edges make up a valid tree. For example: Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], r

## 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), write a function to check whether these edges make up a valid tree. Notice You can assume that no duplicate edges will appear in edges. Since all edg

## [LintCode] Graph Valid Tree

http://www.lintcode.com/en/problem/graph-valid-tree/ DFS 解法: public class Solution { /** * @param n an integer * @param edges a list of undirected edges * @return true if it's a valid tree, or false */ public boolean validTree(int n, int[][] edges) {

## Graph Valid Tree -- LeetCode

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. For example: Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return tru

## 对照层关系图验证代码

包括要在验证期间分析的特定程序集或项目在 “解决方案资源管理器”中,右击建模项目或 “层引用”文件夹,然后单击 “添加引用”.    在 “添加引用”对话框中,选择所需程序集或项目,然后单击 “确定”.    随时手动验证代码从打开的层关系图中验证代码 右击关系图图面,再单击 “验证体系结构”.     说明  默认情况下,层关系图 (.layerdiagram) 文件的 “生成操作”属性设置为 “验证”,以便在验证过程中包括关系图.       “错误列表”窗口会报告发生的任何错误. 有关

## leetcode 261-Graph Valid Tree(medium)（BFS, DFS, Union find）

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. 这道题主要就是判断1.是否有环路 2. 是否是连通图 可以用DFS, BFS 和 Union find,union find最合适. 对于DFS和BFS,首先都是建立