Skip to content

Commit 7ab0e42

Browse files
committed
克隆图
1 parent 5e24baf commit 7ab0e42

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

clone_graph.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)