-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphValidTree.py
More file actions
39 lines (33 loc) · 929 Bytes
/
graphValidTree.py
File metadata and controls
39 lines (33 loc) · 929 Bytes
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
import collections
'''
# Time: O(|V| + |E|)
# Space: O(|V| + |E|)
'''
class solution():
def validTree(self, n, edges):
if len(edges)!=n-1:
return False
# init node's neighbors in dict
neighbors=collections.defaultdict(list)
for u,v in edges:
neighbors[u].append(v)
neighbors[v].append(u)
print(neighbors)
# BFS to check if the graph is valid tree
visited={}
q=collections.deque([0])
print(q)
while q:
curr=q.popleft()
visited[curr]=True
for node in neighbors[curr]:
if node not in visited:
visited[node]=True
q.append(node)
return len(visited)==n
n=5
edges1=[[0,1],[0,2],[0,3],[1,4]]
edges2=[[0,1],[1,2],[2,3],[1,3],[1,4]]
test=solution()
print(test.validTree(n,edges1))
print(test.validTree(n,edges2))