-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path06_graph_valid_tree.py
43 lines (27 loc) · 955 Bytes
/
06_graph_valid_tree.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
35
36
37
38
39
40
41
42
43
class Solution:
def valid_tree(self, n: int, edges: list[list[int]]) -> bool:
if not n:
return True
adj = { i:[] for i in range(n) }
for n1, n2 in edges:
adj[n1].append(n2)
adj[n2].append(n1)
visit = set()
def dfs(i, prev):
if i in visit:
return False
visit.add(i)
for j in adj[i]:
if j == prev: continue
if not dfs(i = j, prev = i):
return False
return True
return dfs(i = 0, prev = -1) and n == len(visit)
if __name__ == "__main__":
obj = Solution()
n1 = 5
edges1 = [[0, 1], [0, 2], [0, 3], [1, 4]]
print(obj.valid_tree(n = n1, edges = edges1))
n2 = 5
edges2 = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]
print(obj.valid_tree(n = n2, edges = edges2))