-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path137.py
40 lines (37 loc) · 1.13 KB
/
137.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
"""
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 cloneGraph(self, node):
# write your code here
if not node:
return
nodeDic = self.bfs(node)
newNodeDic = {}
for label in nodeDic:
newNodeDic[label] = UndirectedGraphNode(label)
for label in nodeDic:
for nNode in nodeDic[label].neighbors:
newNodeDic[label].neighbors.append(newNodeDic[nNode.label])
for i in newNodeDic:
return newNodeDic[i]
def bfs(self, node):
nodeDic = {}
nodeList = []
nodeList.append(node)
while nodeList:
tmpNode = nodeList.pop()
if tmpNode.label not in nodeDic:
nodeDic[tmpNode.label] = tmpNode
for nNode in tmpNode.neighbors:
if nNode.label not in nodeDic:
nodeList.insert(0, nNode)
return nodeDic