# 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 `true`.

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

``` 1 class Solution {
2 public:
3     int find(vector<int> &parent, int x) {
4         if (parent[x] == x) return x;
5         int pa = find(parent, parent[x]);
6         parent[parent[x]] = pa;
7         return pa;
8     }
9     void merge(vector<int> &parent, int x, int y) {
10         int parentX = find(parent, x);
11         int parentY = find(parent, y);
12         if (parentX != parentY)
13             parent[parentY] = parentX;
14     }
15     bool validTree(int n, vector<pair<int, int>>& edges) {
16         vector<int> parent(n, 0);
17         for (int i = 0; i < n; i++) parent[i] = i;
18         for (int i = 0; i < edges.size(); i++) {
19             if (find(parent, edges[i].first) == find(parent, edges[i].second))
20                 return false;
21             merge(parent, edges[i].first, edges[i].second);
22         }
23         return n -1 == edges.size();
24     }
25 };```

## 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

## [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 tru

## 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) {

## 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,首先都是建立