首页 > 代码库 > Clone Graph

Clone Graph

图的复制

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

   }
};


Clone Graph