LeetCode 133 Clone Graph Posted on 2016-03-12 | Question[LeetCode 133] Clone an undirected graph.SubmissionJava Submission12345678910111213141516171819202122232425262728293031/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */public class Solution { public Map<Integer, UndirectedGraphNode> nodes = new HashMap<Integer, UndirectedGraphNode>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return null; } if (nodes.containsKey(node.label)) { return nodes.get(node.label); } UndirectedGraphNode cloneNode = new UndirectedGraphNode(node.label); nodes.put(node.label, cloneNode); for (UndirectedGraphNode neigblor: node.neighbors) { UndirectedGraphNode cloneNeigblor = cloneGraph(neigblor); cloneNode.neighbors.add(cloneNeigblor); } return cloneNode; }}