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.

思路:union find。

是一个树的条件有两个:无环,边数等于节点数减一。

检测无环可以用union find:枚举所有的边,检测并更新他们端点所属的集合。一开始所有的节点都属于各自的集合,然后merge每个边两端点所在的集合。因为树中的点都是连在一起的,最后肯定会在一个集合。注意的是,如果有一条边,一旦将它添加到树中后就会构成一个环,那么这条边的两个端点一定之前已经被添加进了树中(两个点属于同一个集合),否则,每条新加的边,至少有一个端点是之前不存在于这棵树中的(两个点属于不同的集合)。

 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 };
时间: 2024-10-20 20:20:46

Graph Valid Tree -- LeetCode的相关文章

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

LeetCode Graph Valid Tree

原题链接在这里:https://leetcode.com/problems/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

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

261. Graph Valid Tree

也是卡了好多天的题目 主要就是介绍了union-find的算法,用于检查Undirected graph有没有环 http://www.geeksforgeeks.org/union-find/ 1 public boolean validTree(int n, int[][] edges) { 2 int[] parent = new int[n]; 3 Arrays.fill(parent, -1); 4 for(int i = 0; i < edges.length ; i++) { 5

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

Validate Binary Search Tree leetcode java

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with