mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-15 09:57:09 +00:00
Time: 38 ms (85.56%), Space: 18.1 MB (81.8%) - LeetHub
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
"""
|
||||||
|
# Definition for a Node.
|
||||||
|
class Node:
|
||||||
|
def __init__(self, val = 0, neighbors = None):
|
||||||
|
self.val = val
|
||||||
|
self.neighbors = neighbors if neighbors is not None else []
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
from collections import deque
|
||||||
|
class Solution:
|
||||||
|
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
|
||||||
|
if not node:
|
||||||
|
return node
|
||||||
|
|
||||||
|
newHead = Node(node.val)
|
||||||
|
|
||||||
|
frontier = deque([ node ])
|
||||||
|
nodeMapping = { node.val: newHead }
|
||||||
|
while len(frontier) > 0:
|
||||||
|
current = frontier.popleft()
|
||||||
|
|
||||||
|
for n in current.neighbors:
|
||||||
|
if n.val not in nodeMapping:
|
||||||
|
newNode = Node(n.val)
|
||||||
|
nodeMapping[newNode.val] = newNode
|
||||||
|
frontier.append(n)
|
||||||
|
nodeMapping[current.val].neighbors.append(nodeMapping[n.val])
|
||||||
|
|
||||||
|
return newHead
|
||||||
|
|
||||||
Reference in New Issue
Block a user