# leetcode:clone graph

Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.

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 as0. Connect node0to both nodes1and2.
2. Second node is labeled as1. Connect node1to node2.
3. Third node is labeled as2. Connect node2to node2(itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
/ \
/   \
0 --- 2
/ \
\_/
感觉题目很晦涩，没太懂，最后就理解成：就是遍历一个给定的树完全复制建立一个我们自己的树。相当于c++里面的深度复制。
实现：
1.
1 /**
2  * Definition for undirected graph.
3  * struct UndirectedGraphNode {
4  *     int label;
5  *     vector<UndirectedGraphNode *> neighbors;
6  *     UndirectedGraphNode(int x) : label(x) {};
7  * };
8  */
9 class Solution {
10 public:
11     UndirectedGraphNode* cloneGraph(UndirectedGraphNode *node)
12     {
13         if (node == NULL)
14         {
15             return NULL;
16         }
17
18         map<UndirectedGraphNode*, UndirectedGraphNode*> gphMap;
19         queue<UndirectedGraphNode*> gphQue;
20
21         //创建所有顶点
22         gphQue.push(node);
23         while (!gphQue.empty())
24         {
25             UndirectedGraphNode *tmp = gphQue.front();
26             gphQue.pop();
27
28             if (gphMap.find(tmp) == gphMap.end())
29             {
30                 UndirectedGraphNode *newNode = new UndirectedGraphNode(tmp->label);
31                 gphMap[tmp] = newNode;
32
33                 for (int i = 0; i != tmp->neighbors.size(); ++i)
34                 {
35                     gphQue.push(tmp->neighbors[i]);
36                 }
37             }
38         }
39
40         //调整顶点间关系
41         gphQue.push(node);
42         while (!gphQue.empty())
43         {
44             UndirectedGraphNode *tmp = gphQue.front();
45             gphQue.pop();
46
47             UndirectedGraphNode *exitNode = gphMap[tmp];
48             if (exitNode->neighbors.empty() && !tmp->neighbors.empty())
49             {
50                 for (int i = 0; i != tmp->neighbors.size(); ++i)
51                 {
52                     exitNode->neighbors.push_back(gphMap[tmp->neighbors[i]]);
53                     gphQue.push(tmp->neighbors[i]);
54                 }
55             }
56         }
57
58         return gphMap[node];
59     }
60 };
2.BFS实现广度优先搜索
 1 class Solution {
2 public:
3     UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node)
4     {
5         if (node == NULL)
6         {
7             return NULL;
8         }
9
10         map<const UndirectedGraphNode*, UndirectedGraphNode*> gphMap;
11         queue<const UndirectedGraphNode *> gphQue;
12
13         gphQue.push(node);
14         gphMap[node] = new UndirectedGraphNode(node->label);
15         while (!gphQue.empty())
16         {
17             const UndirectedGraphNode *tmp = gphQue.front();
18             gphQue.pop();
19
20             for (int i = 0; i != tmp->neighbors.size(); ++i)
21             {
22                 if (gphMap.find(tmp->neighbors[i]) == gphMap.end())
23                 {
24                     //build the vertex
25                     UndirectedGraphNode *newNode = new UndirectedGraphNode(tmp->neighbors[i]->label);
26                     gphMap[tmp->neighbors[i]] = newNode;
28                     gphQue.push(tmp->neighbors[i]);
29                 }
30                 else
31                 {
32                     gphMap[tmp]->neighbors.push_back(gphMap[tmp->neighbors[i]]);
33                 }
34             }
35         }
36
37         return gphMap[node];
38     }
39 };
3.DFS深度优先搜索
1 class Solution {
2 public:
3     UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node)
4     {
5         if(node == NULL)
6         {
7             return NULL;
8         }
9
10         map<const UndirectedGraphNode*, UndirectedGraphNode*> gphMap;
11
12         return GphClone(node, gphMap); //or return gphMap[node]
13     }
14 private:
15     // DFS
16     static UndirectedGraphNode* GphClone(const UndirectedGraphNode *node,
17         map<const UndirectedGraphNode*, UndirectedGraphNode*> &gphMap)
18     {
19             // a copy already exists
20             if (gphMap.find(node) != gphMap.end())
21             {
22                 return gphMap[node];
23             }
24
25             UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
26             gphMap[node] = newNode;
27
28             for (int i = 0; i != node->neighbors.size(); ++i)
29             {
30                 newNode->neighbors.push_back(GphClone(node->neighbors[i], gphMap));
31             }
32
33             return newNode;
34     }
35 };

• 本文已收录于以下专栏：

举报原因： 您举报文章：leetcode:clone graph 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)