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.

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.

 

这道题居然几乎一遍过了,开心!用union find的方法,先连接每组点,连的过程中先检查,如果已经连过了,就return false;然后都连完一遍再检查一下是不是整个图都是相连的。

public class Solution {
    public boolean validTree(int n, int[][] edges) {
        if (n == 0 || edges == null) {
            return false;
        }
        UF uf = new UF(n);
        for (int i = 0; i < edges.length; i++) {
            int a = edges[i][0];
            int b = edges[i][1];
            if (uf.isConnected(a, b)) {
                return false;
            }
        }
        for (int i = 1; i < n; i++) {
            if (!uf.checkConnect(0, i)) {
                return false;
            }
        }
        return true;
    }
    class UF {
        public int[] father;
        public UF (int n) {
            father = new int[n];
            for (int i = 0; i < n; i++) {
                father[i] = i;
            }
        }
        public int find(int x) {
            if (father[x] == x) {
                return x;
            }
            father[x] = find(father[x]);
            return father[x];
        }
        public boolean isConnected(int a, int b) {
            int roota = find(a);
            int rootb = find(b);
            if (roota != rootb) {
                father[roota] = rootb;
                return false;
            }
            return true;
        }
        public boolean checkConnect(int a, int b) {
            int roota = find(a);
            int rootb = find(b);
            if (roota != rootb) {
                return false;
            }
            return true;
        }
    }
}

不过看了下top solution,人家写的果然精简多了。而且最后不用遍历一遍判断相连,只要判断edges.length是不是等于n - 1即可。真的很精妙啊。

而且可以用Arrays.fill(nums, -1)来fill整个数组。只是要注意,find的时候找到了就不能返回father[x]而是要返回x了。因为这个时候father[x]是-1啊。

public class Solution {
    public boolean validTree(int n, int[][] edges) {
        if (n == 0 || edges == null) {
            return false;
        }
        int[] father = new int[n];
        Arrays.fill(father, -1);
        
        for (int i = 0; i < edges.length; i++) {
            int a = find(father, edges[i][0]);
            int b = find(father, edges[i][1]);
            if (a == b) {
                return false;
            }
            father[a] = b;
        }
        
        return edges.length == n - 1;
    }
    public int find(int[] father, int x) {
        if (father[x] == -1) {
            return x;
        }
        father[x] = find(father, father[x]);
        return father[x];
    }
}

 

 
posted @ 2017-02-10 03:52  璨璨要好好学习  阅读(148)  评论(0编辑  收藏  举报