-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path133.Clone-Graph.py
35 lines (28 loc) · 937 Bytes
/
133.Clone-Graph.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
# https://leetcode.com/problems/clone-graph/description/
#
# algorithms
# Medium (25.08%)
# Total Accepted: 180.2K
# Total Submissions: 718.5K
# beats 50.23% of python submissions
# 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.hash_map = {}
def cloneGraph(self, node):
if not node:
return node
new_node = UndirectedGraphNode(node.label)
self.hash_map[node.label] = new_node
for neighbor in node.neighbors:
if neighbor.label in self.hash_map:
new_node.neighbors.append(self.hash_map[neighbor.label])
else:
new_node.neighbors.append(self.cloneGraph(neighbor))
return new_node