My Python solution with dictionary


  • 6
    Y
       def cloneGraph(self, node):
            if not node:
                return None
            self.newNodeDict={}
            return self.createNode(node)
                    
        def createNode(self, oldNode):
            newNode = UndirectedGraphNode(oldNode.label)
            self.newNodeDict[newNode.label] = newNode
            
            for i in oldNode.neighbors:
                if i.label not in self.newNodeDict:
                    self.createNode(i) #recursively create nodes
                newNode.neighbors.append(self.newNodeDict[i.label])
            return newNode

Log in to reply
 

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