Updated: Thanks for pointing it out @StefanPochmann

```
public class Solution {
public boolean validTree(int n, int[][] edges) {
if(n <= 0)
return false;
if(edges == null || edges.length != n-1)
return false;
int[] unionFind = new int[n];
for(int i = 0 ; i < n ; i++){
unionFind[i] = i;
}
for(int i = 0 ; i < edges.length ; i++){
int x = edges[i][0];
int y = edges[i][1];
if(findRoot(unionFind, x) == findRoot(unionFind, y))
return false;
unionFind[y] = x;
}
return true;
}
int findRoot(int[] unionFind, int i){
while(i != unionFind[i])
i = unionFind[i];
return i;
}
```

}