• Time:O(n)
• Space:O(n)

## C++

``````class Solution {
public:
bool validTree(int n, vector<vector<int>>& edges) {
if (n == 0 || edges.size() != n - 1)
return false;

vector<vector<int>> graph(n);
queue<int> q{{0}};
unordered_set<int> seen{{0}};

for (const auto& e : edges) {
const int u = e[0];
const int v = e[1];
graph[u].push_back(v);
graph[v].push_back(u);
}

while (!q.empty()) {
const int u = q.front();
q.pop();
for (const int v : graph[u])
if (!seen.count(v)) {
q.push(v);
seen.insert(v);
}
}

return seen.size() == n;
}
};
``````

## JAVA

``````class Solution {
public boolean validTree(int n, int[][] edges) {
if (n == 0 || edges.length != n - 1)
return false;

List<Integer>[] graph = new List[n];
Queue<Integer> q = new ArrayDeque<>(Arrays.asList(0));
Set<Integer> seen = new HashSet<>(Arrays.asList(0));

for (int i = 0; i < n; ++i)
graph[i] = new ArrayList<>();

for (int[] e : edges) {
final int u = e[0];
final int v = e[1];
}

while (!q.isEmpty()) {
final int u = q.poll();
for (final int v : graph[u])
if (!seen.contains(v)) {
q.offer(v);
}
}

return seen.size() == n;
}
}
``````

## Python

``````class Solution:
def validTree(self, n: int, edges: List[List[int]]) -> bool:
if n == 0 or len(edges) != n - 1:
return False

graph = [[] for _ in range(n)]
q = deque([0])
seen = {0}

for u, v in edges:
graph[u].append(v)
graph[v].append(u)

while q:
u = q.popleft()
for v in graph[u]:
if v not in seen:
q.append(v)

return len(seen) == n
``````

## Approach 2: UF

• Time:O(n\log n)
• Space:O(n)

## C++

``````class UF {
public:
UF(int n) : count(n), id(n) {
iota(begin(id), end(id), 0);
}

void union_(int u, int v) {
const int i = find(u);
const int j = find(v);
if (i == j)
return;
id[i] = j;
--count;
}

int getCount() const {
return count;
}

private:
int count;
vector<int> id;

int find(int u) {
return id[u] == u ? u : id[u] = find(id[u]);
}
};

class Solution {
public:
bool validTree(int n, vector<vector<int>>& edges) {
if (n == 0 || edges.size() != n - 1)
return false;

UF uf(n);

for (const auto& e : edges)
uf.union_(e[0], e[1]);

return uf.getCount() == 1;
}
};
``````

## JAVA

``````class UF {
public UF(int n) {
count = n;
id = new int[n];
for (int i = 0; i < n; ++i)
id[i] = i;
}

public void union(int u, int v) {
final int i = find(u);
final int j = find(v);
if (i == j)
return;
id[i] = j;
--count;
}

public int getCount() {
return count;
}

private int count;
private int[] id;

private int find(int u) {
return id[u] == u ? u : (id[u] = find(id[u]));
}
}

class Solution {
public boolean validTree(int n, int[][] edges) {
if (n == 0 || edges.length != n - 1)
return false;

UF uf = new UF(n);

for (int[] e : edges)
uf.union(e[0], e[1]);

return uf.getCount() == 1;
}
}
``````

## Python

``````class UF:
def __init__(self, n: int):
self.count = n
self.id = list(range(n))

def union(self, u: int, v: int) -> None:
i = self.find(u)
j = self.find(v)
if i == j:
return
self.id[i] = j
self.count -= 1

def find(self, u: int) -> int:
if self.id[u] != u:
self.id[u] = self.find(self.id[u])
return self.id[u]

class Solution:
def validTree(self, n: int, edges: List[List[int]]) -> bool:
if n == 0 or len(edges) != n - 1:
return False

uf = UF(n)

for u, v in edges:
uf.union(u, v)

return uf.count == 1
``````