Graph Valid Tree
2/16/2018 update Loop through edges, connect nodes parents with union find. If their parents are equal, so there is a loop. Remember to check edges.length == n - 1, Union find can not check number of edges iterated. Solution: Still need to do path compression. It's faster. class Solution { public boolean validTree ( int n, int [][] edges) { int [] roots = new int [n]; for ( int i = 0 ; i < n; i ++ ) { roots[i] = i; } for ( int [] edge : edges) { int x = find(roots, edge[ 0 ]); int y = find(roots, edge[ 1 ]); if (x == y) return false; roots[x] = y; } return edges . length == n - 1 ; } private int find ( int [] roots, int n) { if (roots[n] != n) roots[n] = find(roots, roots[n]); return roots[n]; } } ---------------------------- ---------------------------- -----------------------...