标签:off create solution cti class contain link style ons
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return null; } UndirectedGraphNode result = new UndirectedGraphNode(node.label); Queue<UndirectedGraphNode> queue = new LinkedList<>(); Map<UndirectedGraphNode, UndirectedGraphNode> connections = new HashMap<>(); connections.put(node, result); queue.offer(node); while (!queue.isEmpty()) { UndirectedGraphNode current = queue.poll(); for (UndirectedGraphNode neighbor : current.neighbors) { if (!connections.containsKey(neighbor)) { UndirectedGraphNode newNode = new UndirectedGraphNode(neighbor.label); connections.put(neighbor, newNode); queue.offer(neighbor); } connections.get(current).neighbors.add(connections.get(neighbor)); } } return result; } }
1. Create node when the neighbor is not found.
标签:off create solution cti class contain link style ons
原文地址:http://www.cnblogs.com/shuashuashua/p/7411556.html