class Solution {
private:
unordered_map<int,UndirectedGraphNode*> m;
UndirectedGraphNode *clone(UndirectedGraphNode *node)
{
if(m.find(node->label)!=m.end())return m[node->label];
else
{
UndirectedGraphNode *g=new UndirectedGraphNode(node->label);
m[node->label]=g;
for(int i=0;i<node->neighbors.size();i++)
{
g->neighbors.push_back(cloneGraph(node->neighbors[i]));
}
return g;
}
}
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node)
return NULL;
return clone(node);
}
};
133. Clone Graph
最新推荐文章于 2021-07-30 00:03:36 发布