原题链接在这里: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 = 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, 与Number of Islands II相似.

Check is edges count is equal to n-1(There is only one cluster after merging).

然后判断有没有环,若是find(edge[0],edge[1])返回true 说明edge[0], edge[1]两个点之前就连在一起了.

Time Complexity: O(n*logn). Space: O(n).

AC Java:

  1. public class Solution {
  2. public boolean validTree(int n, int[][] edges) {
  3. if(edges == null || edges.length != n-1){
  4. return false;
  5. }
  6.  
  7. UnionFind tree = new UnionFind(n);
  8. for(int [] edge : edges){
  9. if(!tree.find(edge[0], edge[1])){
  10. tree.union(edge[0], edge[1]);
  11. }else{
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. }
  18.  
  19. class UnionFind{
  20. int count, n;
  21. int [] size;
  22. int [] parent;
  23.  
  24. public UnionFind(int n){
  25. this.n = n;
  26. this.count = n;
  27. size = new int[n];
  28. parent = new int[n];
  29. for(int i = 0; i<n; i++){
  30. parent[i] = i;
  31. size[i] = 1;
  32. }
  33. }
  34.  
  35. public boolean find(int i, int j){
  36. return root(i) == root(j);
  37. }
  38.  
  39. private int root(int i){
  40. while(i != parent[i]){
  41. parent[i] = parent[parent[i]];
  42. i = parent[i];
  43. }
  44. return i;
  45. }
  46.  
  47. public void union(int p, int q){
  48. int i = root(p);
  49. int j = root(q);
  50. if(size[i] > size[j]){
  51. parent[j] = i;
  52. size[i] += size[j];
  53. }else{
  54. parent[i] = j;
  55. size[j] += size[i];
  56. }
  57. this.count--;
  58. }
  59.  
  60. public int size(){
  61. return this.count;
  62. }
  63. }

LeetCode Graph Valid Tree的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. POJ 3276 (开关问题)

    题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...

  2. ACM Arithmetic Expression

    Description Given N arithmetic expressions, can you tell whose result is closest to 9? Input Line 1: ...

  3. 【POJ】3744 Scout YYF I

    http://poj.org/problem?id=3744 题意:直线上n个地雷,n<=10,范围在[1, 100000000],每一次有p的概率向前走一步,1-p的概率向前走两步,问安全通过 ...

  4. Codeforces Round #200 (Div. 2) E. Read Time(二分)

    题目链接 这题,关键不是二分,而是如果在t的时间内,将n个头,刷完这m个磁盘. 看了一下题解,完全不知怎么弄.用一个指针从pre,枚举m,讨论一下.只需考虑,每一个磁盘是从右边的头,刷过来的(左边来的 ...

  5. mysql in 命令

    SQL: select * from table where id IN (3,6,9,1,2,5,8,7); SQL: select * from table where id IN ($str); ...

  6. 谷歌(GDG):智能技术在物联网及移动互联网中的最新应用讲座

       谷歌开发者社区GDG(原谷歌技术用户组GTUG),将于11月23日(周六)下午 1:30-5:00,在北京翠宫饭举办一场智能技术在物联网及移动互联网中的最新应用讲座,培训讲座中将通过三个专题与众 ...

  7. 使用diff制作补丁

    1.制作补丁包 命令格式 diff -uNr  oldfile.c newfile.c > x.patch 2.打补丁 命令格式 patch -p0 < x.patch 总结一下:单个文件 ...

  8. 一个不错的安卓下ssh客户端

    1.使用安卓作为ssh客户端连接ssh服务器 软件名:JuiceSSH 版本   :1.4.8 大小   :4.22 M 百度网盘地址:JuiceSSH_1.4.8.apk  或 JuiceSSH_1 ...

  9. #define is unsafe——I

    I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...

  10. tab1

    <html> <head> <meta charset="UTF-8"> <title>tab</title> < ...