```
public class Solution {
public boolean validTree(int n, int[][] edges) {
if (edges.length != n - 1) return false;
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) {
n--;
root[root1] = root2;
}
}
return n == 1;
}
private int find(int[] root, int key) {
while (root[key] != key) {
root[key] = root[root[key]];
key = root[key];
}
return key;
}
}
```