-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclone-graph.py
More file actions
29 lines (27 loc) · 961 Bytes
/
clone-graph.py
File metadata and controls
29 lines (27 loc) · 961 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
# Definition for a undirected graph node
class UndirectedGraphNode:
def __init__(self, x):
self.label = x
self.neighbors = []
class Solution:
# @param node, a undirected graph node
# @return a undirected graph node
def __init__(self):
self.old2New = {}
def cloneGraph(self, node):
if node == None:
return None
l = [node]
newNodes = []
while l:
n = l.pop(0)
if self.old2New.get(n, None) == None:
self.old2New[n] = UndirectedGraphNode(n.label)
newNode = self.old2New[n]
newNodes.append(newNode)
for neighbor in n.neighbors:
if self.old2New.get(neighbor, None) == None:
self.old2New[neighbor] = UndirectedGraphNode(neighbor.label)
l.append(neighbor)
newNode.neighbors.append(self.old2New[neighbor])
return newNodes[0]