-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClone Graph.py
More file actions
35 lines (25 loc) · 1.07 KB
/
Copy pathClone Graph.py
File metadata and controls
35 lines (25 loc) · 1.07 KB
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
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
class Solution:
def cloneGraph (self, node: 'Node') -> 'Node':
oldToNew = {} #hashmap to {old : clone}
def dfs (node):
if not node:
return None
#check if the clone exists or not, if exists: return the node
if node in oldToNew:
return oldToNew [node]
#if clone does not exist, first make copy of the node and then add the clone to the graph
copy = Node(node.val) #making a clone here
oldToNew[node] = copy #putting the clone against original value in hashmap
for nei in node.neighbors:
copy.neighbors.append(dfs(nei))
return copy
return dfs(node)
#Time complexity = O(n) ~ O(E+V)
# We have to make clone of each nodes so total number of edges and vertices