brief python dfs solution


  • 0
    M
    class Solution:
        # @param node, a undirected graph node
        # @return a undirected graph node
        
        def __init__(self):
            self.nd={}
        def cloneGraph(self, node):
            if not node :return None
            cur = UndirectedGraphNode(node.label)
            self.nd[cur.label]=cur
            for i in node.neighbors:
                if i.label==node.label:
                    cur.neighbors.append(cur)
                    continue
                if i.label in self.nd:
                    cur.neighbors.append(self.nd[i.label])
                else:
                    ret = self.cloneGraph(i)
                    if ret!=None :cur.neighbors.append(ret)
            return cur
    

Log in to reply
 

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