力扣算法——133.CloneGraph【M】

Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.

Example:

Input:
{"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1}

Explanation:
Node 1's value is 1, and it has two neighbors: Node 2 and 4.
Node 2's value is 2, and it has two neighbors: Node 1 and 3.
Node 3's value is 3, and it has two neighbors: Node 2 and 4.
Node 4's value is 4, and it has two neighbors: Node 1 and 3.

Note:

  1. The number of nodes will be between 1 and 100.
  2. The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
  3. Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
  4. You must return the copy of the given node as a reference to the cloned graph.

Soulution:

  使用DFS和BFS即可

  两种方法在leetcode中都能通过,但在牛客中,BFS使用的而外空间超出限制,所以只能使用DFS

  

 1 struct UndirectedGraphNode {
 2     int label;
 3     vector<UndirectedGraphNode *> neighbors;
 4     UndirectedGraphNode(int x) : label(x) {};
 5 };
 6 
 7 //DFS
 8 class Solution01 {
 9 public:
10     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
11         unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
12         return helper(node, mapR);
13     }
14     UndirectedGraphNode* helper(UndirectedGraphNode*node, unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>&mapR)
15     {
16         if (node == nullptr)return nullptr;
17         if (mapR.find(node) != mapR.end())//旧节点
18             return mapR[node];
19         UndirectedGraphNode* res = new UndirectedGraphNode(node->label);//新节点
20         mapR[node] = res;
21         for (auto a : node->neighbors)
22             res->neighbors.push_back(helper(a, mapR));
23         return res;
24     }
25 };
26 //BFS
27 class Solution02 {
28 public:
29     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
30         queue<UndirectedGraphNode*>qNode;
31         qNode.push(node);
32         unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
33         UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
34         mapR[node] = res;
35         while (!qNode.empty())
36         {
37             UndirectedGraphNode *p = qNode.front();
38             qNode.pop();
39             for (auto a : p->neighbors)
40             {
41                 if (mapR.find(a) == mapR.end())//新节点
42                 {
43                     qNode.push(a);//加入
44                     mapR[a] = new UndirectedGraphNode(a->label);//新节点
45                 }
46                 mapR[p]->neighbors.push_back(mapR[a]);//连接节点
47             }
48         }
49         return res;        
50     }
51 };
原文地址:https://www.cnblogs.com/zzw1024/p/11839160.html