题目链接
思路
代码
class Solution {
public Node cloneGraph(Node node) {
if(node == null){
return node;
}
HashMap<Node, Node> visited = new HashMap();
LinkedList<Node> queue = new LinkedList<Node>();
queue.add(node);
visited.put(node, new Node(node.val, new ArrayList()));
while(!queue.isEmpty()){
Node n = queue.remove();
for(Node neighbor : n.neighbors){
if(!visited.containsKey(neighbor)){
visited.put(neighbor, new Node(neighbor.val, new ArrayList()));
queue.add(neighbor);
}
// renew current node's neighbor list
visited.get(n).neighbors.add(visited.get(neighbor));
}
}
return visited.get(node);
}
}
标签:node,133,queue,Node,BFS,neighbor,new,visited,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17048022.html