```
public boolean validTree(int n, int[][] edges) {
int[] root = new int[n];
for(int i = 0; i < n; i++) {
root[i] = i;
}
for(int[] edge : edges) {
int root1 = find(root, edge[0]);
int root2 = find(root, edge[1]);
if(root1 == root2) {
return false;
}
root[root1] = root2;
}
return edges.length == n - 1;
}
private int find(int[] root, int p) {
while(root[p] != p) {
root[p] = root[root[p]];
p = root[p];
}
return p;
}
```