```
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
unordered_map<UndirectedGraphNode *, int> visited;
if(!node)
return NULL;
UndirectedGraphNode *clone = new UndirectedGraphNode(node->label);
return recurse(node, visited, clone);
}
UndirectedGraphNode *recurse(UndirectedGraphNode *node, unordered_map<UndirectedGraphNode *, int> &visited, UndirectedGraphNode* &clone)
{
if(!node)
return NULL;
if(visited[node])
{
return NULL;
}
for(int i = 0; i < node->neighbors.size(); i++)
{
clone->neighbors.push_back(new UndirectedGraphNode(node->neighbors[i]->label));
}
visited[node] = 1;
for(int i = 0; i < node->neighbors.size(); i++)
{
recurse(node->neighbors[i], visited, clone->neighbors[i]);
}
return clone;
}
};
```