6 lines in python

  • 2
    class Solution:
        def cloneGraph(self, node):
            return self.dfs(node, {})
        def dfs(self, node, d):
            if not node: return None
            if node not in d:
                d[node] = UndirectedGraphNode(node.label)
                d[node].neighbors += [self.dfs(n, d) for n in node.neighbors]
            return d[node]

  • 0

    beautiful solution

  • 0
    This post is deleted!

Log in to reply

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