We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5e24baf commit 7ab0e42Copy full SHA for 7ab0e42
clone_graph.py
@@ -0,0 +1,19 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ # @param node, a undirected graph node
5
+ # @return a undirected graph node
6
+ def __init__(self):
7
+ self.dict = {} # 存放已复制的节点
8
9
+ def cloneGraph(self, node):
10
+ if not node:
11
+ return None
12
+ # write your code here
13
+ if node in self.dict:
14
+ return self.dict[node] # 该节点已复制
15
+ ret = UndirectedGraphNode(node.label)
16
+ self.dict[node] = ret
17
+ for neighbor in node.neighbors:
18
+ ret.neighbors.append(self.cloneGraph(neighbor))
19
+ return ret
0 commit comments