133. Clone Graph

Total Accepted: 65055  Total Submissions: 261527  Difficulty: Medium

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use  # as a separator for each node, and  , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/

分析:

本题要求将给定图完整复制出来!

用映射来保存当前定点编号label和其对应(就是他自身这个节点的结构体指针)节点的关系。

那么从当前节点(编号)开始,如果不在map中就将当前节点(整个结构体)new 一下为其分配内存,然后将原图中的邻节点(注意是结构体)复制到当前节点的邻点。

可以选广度遍历方式---接着就是访问当前节点的相邻点。

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(node==NULL)
            return NULL;
        unordered_map<int,UndirectedGraphNode*>  mapping;  
        queue<UndirectedGraphNode*> que;
        mapping[node->label] =new UndirectedGraphNode(node->label);
        que.push(node);
        while(!que.empty())
        {
            UndirectedGraphNode* curNode = que.front();
            que.pop();
            for (auto padjNode : curNode->neighbors)//遍历向量neighbors中的每一个元素(为UndirectedGraphNode *型指针变量)
            {
                if (mapping.find(padjNode->label) == mapping.end())//当前顶点是否在map中,如果不在就为其分配内存
                {
                    mapping[padjNode->label] = new UndirectedGraphNode(padjNode->label);
                    que.push(padjNode);
                }
                mapping[curNode->label]->neighbors.push_back(mapping[padjNode->label]);
            }
        }
        return mapping[node->label];
    }
};

也可以选深度遍历方式

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(node==NULL)  
            return NULL;  
        if(mapping.find(node->label)!=mapping.end())
            return mapping[node->label];
        UndirectedGraphNode *nodeClone = new UndirectedGraphNode(node->label);
        mapping[node->label] = nodeClone;
        for(int st = 0; st < node->neighbors.size(); st ++)
        {
            UndirectedGraphNode *temp = cloneGraph(node->neighbors[st]);
            if(temp != NULL)
                nodeClone->neighbors.push_back(temp);
        }
        return nodeClone;    
    }
private:
     unordered_map<int,UndirectedGraphNode*>  mapping;  
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/51218500

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

你可能感兴趣的:(LeetCode,算法,技术,面试,回溯法)