Clean Python DFS


  • 0
    W
    class Solution:
    
        def cloneGraph(self, node):
            self.seen = {}
            if node:
                return self.cloneNode(node)
        
        def cloneNode(self, node):
            if node.label in self.seen:
                return self.seen[node.label]
            clonedNode = UndirectedGraphNode(node.label)
            self.seen[node.label] = clonedNode
            clonedNode.neighbors = [self.cloneNode(n) for n in node.neighbors]
            return clonedNode
    

Log in to reply
 

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