-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path261_Graph_Valid_Tree.py
More file actions
33 lines (25 loc) · 939 Bytes
/
261_Graph_Valid_Tree.py
File metadata and controls
33 lines (25 loc) · 939 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
# 1 Possible Solutions
# 1. DFS
class Solution:
# DFS
# Time: O(E+V), Space: O(E+V)
def validTree(self, n: int, edges: List[List[int]]) -> bool:
# Empty Tree is still a valid Tree
if not n:
return True
adjacencyList = {i:[] for i in range(n)}
visited = set()
for n1, n2 in edges:
adjacencyList[n1].append(n2)
adjacencyList[n2].append(n1)
return self.traverseGraph(0, -1, visited, adjacencyList) and n == len(visited)
def traverseGraph(self, node, prev, visited, adjacencyList):
if node in visited:
return False
visited.add(node)
for neighbour in adjacencyList[node]:
if neighbour == prev:
continue
if not self.traverseGraph(neighbour, node, visited, adjacencyList):
return False
return True