/**
* Definition for a Node.
* type Node struct {
* Val int
* Neighbors []*Node
* }
*/
func cloneGraph(node *Node) *Node {
if node==nil{
return nil
}
res := &Node{Val: node.Val}
tmpMap := map[*Node]*Node{node.Val: res}
queue := []*Node{node}
for len(queue) > 0 {
item := queue[0]
queue = queue[1:]
neighbors := make([]*Node, len(item.Neighbors))
for idx, sub := range item.Neighbors {
if tmpMap[sub.Val] != nil {
neighbors[idx] = tmpMap[sub.Val]
} else {
subNode := &Node{Val: sub.Val}
tmpMap[subNode.Val] = subNode
neighbors[idx] = subNode
queue = append(queue, sub)
}
}
tmpMap[item.Val].Neighbors=neighbors
}
return res
}