|
| 1 | +// 133. clone graph |
| 2 | + |
| 3 | +/* |
| 4 | +// Definition for a Node. |
| 5 | +class Node { |
| 6 | +public: |
| 7 | + int val; |
| 8 | + vector<Node*> neighbors; |
| 9 | + Node() { |
| 10 | + val = 0; |
| 11 | + neighbors = vector<Node*>(); |
| 12 | + } |
| 13 | + Node(int _val) { |
| 14 | + val = _val; |
| 15 | + neighbors = vector<Node*>(); |
| 16 | + } |
| 17 | + Node(int _val, vector<Node*> _neighbors) { |
| 18 | + val = _val; |
| 19 | + neighbors = _neighbors; |
| 20 | + } |
| 21 | +}; |
| 22 | +*/ |
| 23 | +// my impl |
| 24 | +// 1. dfs。时间复杂度:O(N),空间复杂度:O(N)+ O(H)栈的高度。 |
| 25 | +class Solution { |
| 26 | + public: |
| 27 | + unordered_map<Node*, Node*> visited; //<原始节点,copy的节点>, 记录已经clone的节点 |
| 28 | + Node* cloneGraph(Node* node) { |
| 29 | + if (node == nullptr) return node; |
| 30 | + // 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回 |
| 31 | + if (visited.find(node) != visited.end()) { |
| 32 | + return visited[node]; |
| 33 | + } |
| 34 | + // 深copy |
| 35 | + Node* cloned_node = new Node(node->val); |
| 36 | + // 注意标记visited,否则死循环 |
| 37 | + visited[node] = cloned_node; |
| 38 | + // 遍历该节点的邻居并更新cloned_node的邻居列表 |
| 39 | + for (const auto& neighbor : node->neighbors) { |
| 40 | + cloned_node->neighbors.emplace_back(cloneGraph(neighbor)); |
| 41 | + } |
| 42 | + return cloned_node; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +// 2. bfs。时间复杂度:O(N),空间复杂度:O(N)。 |
| 47 | +class Solution { |
| 48 | + public: |
| 49 | + Node* cloneGraph(Node* node) { |
| 50 | + if (node == nullptr) return node; |
| 51 | + |
| 52 | + unordered_map<Node*, Node*> copied; //<原始节点,copy的节点>, 记录已经clone的节点 |
| 53 | + queue<Node*> que; |
| 54 | + // 根节点先入队 |
| 55 | + que.push(node); |
| 56 | + Node* cloned_node = new Node(node->val); |
| 57 | + copied[node] = cloned_node; |
| 58 | + |
| 59 | + while (!que.empty()) { |
| 60 | + Node* curr = que.front(); |
| 61 | + que.pop(); |
| 62 | + // 遍历该节点的邻居并更新cloned_node的邻居列表 |
| 63 | + for (const auto& neighbor : curr->neighbors) { |
| 64 | + if (copied.find(neighbor) == copied.end()) { |
| 65 | + Node* new_node = new Node(neighbor->val); |
| 66 | + copied[neighbor] = new_node; |
| 67 | + // 将邻居节点加入队列中 |
| 68 | + que.push(neighbor); |
| 69 | + } |
| 70 | + // 将邻居节点加入队列中 |
| 71 | + copied[curr]->neighbors.emplace_back(copied[neighbor]); |
| 72 | + } |
| 73 | + } |
| 74 | + return cloned_node; |
| 75 | + } |
| 76 | +}; |
| 77 | + |
1 | 78 | // Source : https://oj.leetcode.com/problems/clone-graph/
|
2 | 79 | // Author : Hao Chen
|
3 | 80 | // Date : 2014-10-12
|
4 | 81 |
|
5 |
| -/********************************************************************************** |
6 |
| -* |
7 |
| -* Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. |
8 |
| -* |
9 |
| -* OJ's undirected graph serialization: |
10 |
| -* |
11 |
| -* Nodes are labeled uniquely. |
12 |
| -* |
13 |
| -* We use # as a separator for each node, and , as a separator for node label and each neighbor of the node. |
14 |
| -* |
15 |
| -* As an example, consider the serialized graph {0,1,2#1,2#2,2}. |
16 |
| -* |
17 |
| -* The graph has a total of three nodes, and therefore contains three parts as separated by #. |
18 |
| -* |
19 |
| -* First node is labeled as 0. Connect node 0 to both nodes 1 and 2. |
20 |
| -* Second node is labeled as 1. Connect node 1 to node 2. |
21 |
| -* Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle. |
22 |
| -* |
23 |
| -* Visually, the graph looks like the following: |
24 |
| -* |
25 |
| -* 1 |
26 |
| -* / \ |
27 |
| -* / \ |
28 |
| -* 0 --- 2 |
29 |
| -* / \ |
30 |
| -* \_/ |
31 |
| -* |
32 |
| -* |
33 |
| -**********************************************************************************/ |
| 82 | +/********************************************************************************** |
| 83 | + * |
| 84 | + * Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. |
| 85 | + * |
| 86 | + * OJ's undirected graph serialization: |
| 87 | + * |
| 88 | + * Nodes are labeled uniquely. |
| 89 | + * |
| 90 | + * We use # as a separator for each node, and , as a separator for node label and each neighbor of |
| 91 | + *the node. |
| 92 | + * |
| 93 | + * As an example, consider the serialized graph {0,1,2#1,2#2,2}. |
| 94 | + * |
| 95 | + * The graph has a total of three nodes, and therefore contains three parts as separated by #. |
| 96 | + * |
| 97 | + * First node is labeled as 0. Connect node 0 to both nodes 1 and 2. |
| 98 | + * Second node is labeled as 1. Connect node 1 to node 2. |
| 99 | + * Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle. |
| 100 | + * |
| 101 | + * Visually, the graph looks like the following: |
| 102 | + * |
| 103 | + * 1 |
| 104 | + * / \ |
| 105 | + * / \ |
| 106 | + * 0 --- 2 |
| 107 | + * / \ |
| 108 | + * \_/ |
| 109 | + * |
| 110 | + * |
| 111 | + **********************************************************************************/ |
34 | 112 |
|
35 | 113 | /**
|
36 | 114 | * Definition for undirected graph.
|
|
41 | 119 | * };
|
42 | 120 | */
|
43 | 121 | class Solution {
|
44 |
| -public: |
45 |
| - UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { |
46 |
| - if (node == NULL) return NULL; |
47 |
| - |
48 |
| - //create a map, key is source node, value is clone node. |
49 |
| - map<UndirectedGraphNode*, UndirectedGraphNode*> cloneMap; |
50 |
| - |
51 |
| - //using queue for breadth first search |
52 |
| - queue<UndirectedGraphNode*> q; |
53 |
| - q.push(node); |
54 |
| - |
55 |
| - //clone the root |
56 |
| - UndirectedGraphNode* cloneNode = new UndirectedGraphNode(node->label); |
57 |
| - cloneMap[node] = cloneNode; |
58 |
| - |
59 |
| - //breadth first search |
60 |
| - while(!q.empty()){ |
61 |
| - UndirectedGraphNode* n = q.front(); |
62 |
| - q.pop(); |
63 |
| - //for each neighbors |
64 |
| - for(int i=0; i<n->neighbors.size(); i++){ |
65 |
| - UndirectedGraphNode* neighbor= n->neighbors[i]; |
66 |
| - //not existed in cloneMap |
67 |
| - if (cloneMap.find(neighbor)==cloneMap.end()){ |
68 |
| - //clone a node |
69 |
| - UndirectedGraphNode* newNode = new UndirectedGraphNode(neighbor->label); |
70 |
| - cloneMap[n]->neighbors.push_back(newNode); |
71 |
| - cloneMap[neighbor] = newNode; |
72 |
| - |
73 |
| - //put the neighbors into the queue |
74 |
| - q.push(neighbor); |
75 |
| - }else{ |
76 |
| - cloneMap[n]->neighbors.push_back(cloneMap[neighbor]); |
77 |
| - } |
78 |
| - } |
| 122 | + public: |
| 123 | + UndirectedGraphNode* cloneGraph(UndirectedGraphNode* node) { |
| 124 | + if (node == NULL) return NULL; |
| 125 | + |
| 126 | + // create a map, key is source node, value is clone node. |
| 127 | + map<UndirectedGraphNode*, UndirectedGraphNode*> cloneMap; |
| 128 | + |
| 129 | + // using queue for breadth first search |
| 130 | + queue<UndirectedGraphNode*> q; |
| 131 | + q.push(node); |
| 132 | + |
| 133 | + // clone the root |
| 134 | + UndirectedGraphNode* cloneNode = new UndirectedGraphNode(node->label); |
| 135 | + cloneMap[node] = cloneNode; |
| 136 | + |
| 137 | + // breadth first search |
| 138 | + while (!q.empty()) { |
| 139 | + UndirectedGraphNode* n = q.front(); |
| 140 | + q.pop(); |
| 141 | + // for each neighbors |
| 142 | + for (int i = 0; i < n->neighbors.size(); i++) { |
| 143 | + UndirectedGraphNode* neighbor = n->neighbors[i]; |
| 144 | + // not existed in cloneMap |
| 145 | + if (cloneMap.find(neighbor) == cloneMap.end()) { |
| 146 | + // clone a node |
| 147 | + UndirectedGraphNode* newNode = new UndirectedGraphNode(neighbor->label); |
| 148 | + cloneMap[n]->neighbors.push_back(newNode); |
| 149 | + cloneMap[neighbor] = newNode; |
| 150 | + |
| 151 | + // put the neighbors into the queue |
| 152 | + q.push(neighbor); |
| 153 | + } else { |
| 154 | + cloneMap[n]->neighbors.push_back(cloneMap[neighbor]); |
79 | 155 | }
|
80 |
| - |
81 |
| - return cloneNode; |
| 156 | + } |
82 | 157 | }
|
| 158 | + |
| 159 | + return cloneNode; |
| 160 | + } |
83 | 161 | };
|
0 commit comments