classSolution:defvalidTree(self,n:int,edges: List[List[int]]) ->bool:iflen(edges)!= n -1:returnFalse graph = [[] for _ inrange(n)]for edge in edges: graph[edge[0]].append(edge[1]) graph[edge[1]].append(edge[0]) seen =set()defdfs(node,parent):if node in seen:return seen.add(node)for nei in graph[node]:if nei == parent:continueif nei in seen:returnFalse res =dfs(nei, node)ifnot res:returnFalsereturnTruereturndfs(0, -1)andlen(seen)== n