-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphValidTree.py
34 lines (30 loc) · 915 Bytes
/
GraphValidTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def validTree(self, n: int, edges: List[List[int]]) -> bool:
'''
if circle exist in undirected graph
dfs
'''
def add_to_graph(a,b):
graph[a].append(b)
def dfs(pred, node):
visited.add(node)
for i in graph[node]:
if i != pred:
if i in visited:
return True
else:
if dfs(node, i):
return True
return False
graph = []
for i in range(n):
graph.append([])
visited = set()
for i in edges:
add_to_graph(i[0], i[1])
add_to_graph(i[1], i[0])
if dfs(-1, 0):
return False
if len(visited) != n:
return False
return True