-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.py
30 lines (29 loc) · 864 Bytes
/
Solution.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
#coding=utf-8
__author__ = 'xuxuan'
# Definition for a undirected graph node
class UndirectedGraphNode(object):
def __init__(self, x):
self.label = x
self.neighbors = []
class Solution(object):
_map={}
def cloneGraph(self, node):
"""
:type node: UndirectedGraphNode
:rtype: UndirectedGraphNode
"""
self._map={}
if node:
self.f(node)
keys=self._map.keys()
for key in keys:
a=self._map[key]
for nei in key.neighbors:
a.neighbors.append(self._map[nei])
return self._map[node]
return None
def f(self,node):
if node and not self._map.get(node) :
self._map[node]=UndirectedGraphNode(node.label)
for nei in node.neighbors:
self.f(nei)