Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. code example
Example: Clone Graph
HashMap<Node, Node> map = new HashMap<>();
public Node cloneGraph(Node node) {
map.put(node, new Node(node.val, new ArrayList<>()));
for(Node neighbor: node.neighbors){
if(map.containsKey(neighbor)){
map.get(node).neighbors.add(map.get(neighbor));
}else{
map.get(node).neighbors.add(cloneGraph(neighbor));
}
}
return map.get(node);
}