Python solution


  • 1
    F

    By the way, is the graph directed actually

    # Definition for a undirected graph node
    # class UndirectedGraphNode(object):
    #     def __init__(self, x):
    #         self.label = x
    #         self.neighbors = []
    
    class Solution(object):
        def __init__(self):
            self.visited = {}
        def cloneGraph(self, node):
            """
            :type node: UndirectedGraphNode
            :rtype: UndirectedGraphNode
            """
            if node == None: return None
            graph = UndirectedGraphNode(node.label)
            self.visited[node.label] = graph
            for n in node.neighbors:
                if n.label in self.visited:
                    graph.neighbors.append(self.visited[n.label])
                else:
                    graph.neighbors.append(self.cloneGraph(n))
            return graph

  • 0
    Z

    from the problem statement I think it is undirected.


Log in to reply
 

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