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;
27                     gphMap[tmp]->neighbors.push_back(newNode);            //Adjust the Vertex    
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 };


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值