Clone Graph

c++

dfs

// LeetCode, Clone Graph
// DFS,时间复杂度 O(n),空间复杂度 O(n)
class Solution {
public:
    UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node) {
         if(node == nullptr) return nullptr;
         // key is original node,value is copied node
         unordered_map<const UndirectedGraphNode *,
                              UndirectedGraphNode *> copied;
         clone(node, copied);
         return copied[node];
    }
private:
    // DFS
    static UndirectedGraphNode* clone(const UndirectedGraphNode *node,
             unordered_map<const UndirectedGraphNode *,
             UndirectedGraphNode *> &copied) {
         // a copy already exists
         if (copied.find(node) != copied.end()) return copied[node];
         UndirectedGraphNode *new_node = new UndirectedGraphNode(node->label);
         copied[node] = new_node;
         for (auto nbr : node->neighbors)
             new_node->neighbors.push_back(clone(nbr, copied));
         return new_node;
    }
};

bfs

class Solution {
public:
    UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node) {
        if (node == nullptr) return nullptr;
        // key is original node,value is copied node
        unordered_map<const UndirectedGraphNode *,
                             UndirectedGraphNode *> copied;
        // each node in queue is already copied itself
        // but neighbors are not copied yet
        queue<const UndirectedGraphNode *> q;
        q.push(node);
        copied[node] = new UndirectedGraphNode(node->label);
        while (!q.empty()) {;
            const UndirectedGraphNode *cur = q.front();
            q.pop();
            for (auto nbr : cur->neighbors) {
                 // a copy already exists
                 if (copied.find(nbr) != copied.end()) {
                     copied[cur]->neighbors.push_back(copied[nbr]);
             } else {
                 UndirectedGraphNode *new_node =
                          new UndirectedGraphNode(nbr->label);
                 copied[nbr] = new_node;
                 copied[cur]->neighbors.push_back(new_node);
                 q.push(nbr);
             }
         }
     }
     return copied[node];
   }
};



java


/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
public class Solution{
Map<UndirectedGraphNode,UndirectedGraphNode> map=new HashMap();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node){
if(node==null)
return null;
if(map.containsKey(node)){
return map.get(node);
}
UndirectedGraphNode ug=new UndirectedGraphNode(node.label);
map.put(node,ug);
for(int i=0;i<node.neighbors.size();i++){
ug.neighbors.add(cloneGraph(node.neighbors.get(i)));
}
return ug;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值