Участник:F.Nikitin/CloneGraph

Материал из DISCOPAL
Перейти к: навигация, поиск
class NewNode:
    def __init__(self, x):
        self.val = x
        self.neighbors = []
 
class Solution:
    def cloneGraph(self, node):
        if node is None: return None
        cloned, queue = {node:NewNode(node.val)}, [node]
 
        while queue:
            current = queue.pop()
            for neighbor in current.neighbors:
                if neighbor not in cloned:
                    queue.append(neighbor)
                    cloned_neighbor = NewNode(neighbor.val)
                    cloned[neighbor] = cloned_neighbor
                cloned[current].neighbors.append(cloned[neighbor])
        return cloned[node]