Concise python solution


  • 0
    Y
    def cloneGraph(self, node):
        seen = {}
        return self.clone_graph_help(node, seen)
    
    def clone_graph_help(self, node, seen):
        if not node:
            return
        
        if node in seen:
            return seen[node]
    
        new_node = UndirectedGraphNode(node.label)
        seen[node] = new_node
        for n in node.neighbors:
            new_node.neighbors.append(self.clone_graph_help(n, seen))
        return new_node

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.