题目:
题解:
func cloneGraph(node *Node) *Node {if node == nil {return node}visited := map[*Node]*Node{}// 将题目给定的节点添加到队列queue := []*Node{node}// 克隆第一个节点并存储到哈希表中visited[node] = &Node{node.Val, []*Node{}}// 广度优先搜索for len(queue) > 0 {// 取出队列的头节点n := queue[0]// 遍历该节点的邻居queue = queue[1:]for _, neighbor := range n.Neighbors {if _, ok := visited[neighbor]; !ok {// 如果没有被访问过,就克隆并存储在哈希表中visited[neighbor] = &Node{neighbor.Val, []*Node{}}// 将邻居节点加入队列中queue = append(queue, neighbor)}// 更新当前节点的邻居列表visited[n].Neighbors = append(visited[n].Neighbors, visited[neighbor])}}return visited[node]
}