Please help find the bug: P133 Clone Graph


  • 0
    Z

    my code is as follows:

       def cloneGraph(self, node):         
        if node == None:
            return None
        visited = set()
        q = [node]
        
        res_node = UndirectedGraphNode(node.label)
        res_q = [res_node]
        tmp_node = None
        
        while len(q) > 0:
            node = q.pop(0)
            visited.add(node.label)
            tmp_node = res_q.pop(0)
            # print('tmp_node.label=%d, node.label=%d'%(tmp_node.label, node.label))
            for neighbor in node.neighbors[::-1]:
                res_neighbor = UndirectedGraphNode(neighbor.label)
                tmp_node.neighbors.insert(0, res_neighbor)
                # print(neighbor.label)
                if neighbor.label not in visited:
                    q.insert(0, neighbor)
                    # visited.add(neighbor.label)
                    res_q.insert(0, res_neighbor)
        return res_node

Log in to reply
 

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