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

LeetCode Clone Graph

时间:2014-07-22 23:05:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   re   

class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        UndirectedGraphNode *clone = dfs(node);
        dfs_clear(node);
        return clone;
    }
    
    UndirectedGraphNode *dfs(UndirectedGraphNode *orgin) {
        if (orgin == NULL) return NULL;
        UndirectedGraphNode *clone = get_clone_node(orgin);
        if (clone != NULL) return clone;
        // cout<<"cloning node :"<<orgin->label<<endl;
        int olen = orgin->neighbors.size();
        
        clone = add_clone_node(orgin);
        
        for (int i=0; i<olen; i++) {
            UndirectedGraphNode *nb = orgin->neighbors[i];
            clone->neighbors.push_back(dfs(nb));
        }
        return clone;
    }
    
    void dfs_clear(UndirectedGraphNode *node) {
        UndirectedGraphNode *clone = get_clone_node(node);
        if (clone == NULL) return;
        // cout<<"clear node: "<<node->label<<endl;
        del_clone_node(node);
        
        int len = node->neighbors.size();
        
        for (int i=0; i<len; i++) {
            dfs_clear(node->neighbors[i]);
        }
    }
    
    UndirectedGraphNode *get_clone_node(UndirectedGraphNode *node) {
        int len;
        // cout<<"get info of cloned node : "<<node->label<<endl;
        if (node == NULL || (len = node->neighbors.size()) < 2
            || node->neighbors[len - 2] != NULL) return NULL;
        return node->neighbors[len - 1];
    }
    
    UndirectedGraphNode *add_clone_node(UndirectedGraphNode *node) {
        if (node == NULL) return NULL;
        // cout<<"append link for cloned node: "<<node->label<<endl;
        node->neighbors.push_back(NULL);
        node->neighbors.push_back(new UndirectedGraphNode(node->label));
        return node->neighbors.back();
    }
    
    void del_clone_node(UndirectedGraphNode *node) {
        if (get_clone_node(node) == NULL) return;
        // cout<<"del clone link for node:"<<node->label<<endl;
        node->neighbors.pop_back(); // cloned node addr
        node->neighbors.pop_back(); // null
    }
};

dfs扫描复制,通过在neighbor数组中加入NULL指针来标记已复制与未复制的节点,同时在NULL项后存储该节点对应的克隆节点,最后将原有节点还原。240ms

类似的可以用一个hash表来存储新节点地址与对应老节点的对应关系,同时进行连接关系的复制。当然两种方法的dfs可以改成bfs,防止栈溢出。

class Solution {
private:
    unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> o2n;
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        o2n.clear();
        UndirectedGraphNode *clone = dfs(node);
        return clone;
    }
    
    UndirectedGraphNode *dfs(UndirectedGraphNode *node) {
        if (node == NULL) return NULL;
        unordered_map<UndirectedGraphNode *, UndirectedGraphNode *>::iterator iter = o2n.find(node);
        if (iter != o2n.end()) return iter->second;
        
        UndirectedGraphNode *clone = new UndirectedGraphNode(node->label);
        o2n.insert(make_pair(node, clone));
        
        int len = node->neighbors.size();
        
        for (int i=0; i<len; i++) {
            clone->neighbors.push_back(dfs(node->neighbors[i]));
        }
        return clone;
    }
};

LeetCode Clone Graph,布布扣,bubuko.com

LeetCode Clone Graph

标签:style   blog   color   io   for   re   

原文地址:http://www.cnblogs.com/lailailai/p/3861309.html

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