-
Notifications
You must be signed in to change notification settings - Fork 0
clone graph
Tim_Gao edited this page Sep 14, 2016
·
4 revisions
private HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null){
return null;
}
if (hm.containsKey(node)){
return hm.get(node);
}
UndirectedGraphNode n = new UndirectedGraphNode(node.label);
for (UndirectedGraphNode crt : node.neighbors){
n.neighbors.add(cloneGraph(crt));
}
hm.put(node, n);
return n;
}
}