码迷,mamicode.com
首页 > 其他好文 > 详细

Clone Graph

时间:2015-01-01 21:19:33      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:

图的复制

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

标签:

原文地址:http://blog.csdn.net/sina012345/article/details/42321189

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!