Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class CloneGraph { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return null; } Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); UndirectedGraphNode newHead = new UndirectedGraphNode(node.label); queue.add(node); map.put(node, newHead); while (!queue.isEmpty()) { UndirectedGraphNode curr = queue.poll(); for (UndirectedGraphNode n : curr.neighbors) { if (!map.containsKey(n)) { UndirectedGraphNode copy = new UndirectedGraphNode(n.label); map.put(n, copy); queue.add(n); } map.get(curr).neighbors.add(map.get(n)); } } return newHead; } }
|