题目描述:
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 #.
First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
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
/ \
\_/
解题方法
题目给定的无向图节点的结构如下:
struct UndirectedGraphNode { int label; vector<UndirectedGraphNode *> neighbors; UndirectedGraphNode(int x) : label(x) {}; };
要克隆一个已有的图,需要将已有的图进行一遍遍历,此处我们采用BFS,通过队列来实现。一个队列用来存储已有的图,一个队列用来存储克隆图。每次把一个已有图节点放入队列中,都相应创建一个克隆图新节点放入克隆队列,知道已有图队列为空。此时要注意需要使用一个容器存储已经遍历的节点,防止重复加入队列。而对于已有节点,克隆图需要找到队列中已有节点的位置,并使得指针指向该位置。这是通过一个链表来实现的。详细代码如下:
class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == nullptr) return node; //存放已访问过节点的容器 set<int> visit; //一个用于遍历,一个用于克隆 queue<UndirectedGraphNode * > initial_queue, result_queue; //如果遍历到某节点指向已在队列中的节点,需要用list进行指针指向。 list<UndirectedGraphNode * > copy_list; initial_queue.push(node); visit.insert(node->label); UndirectedGraphNode * result = new UndirectedGraphNode(node->label); result_queue.push(result); copy_list.push_back(result); while (!initial_queue.empty()) { UndirectedGraphNode * temp1 = initial_queue.front(); initial_queue.pop(); UndirectedGraphNode * temp2 = result_queue.front(); result_queue.pop(); //copy_queue.pop(); for (int i = 0; i < temp1->neighbors.size(); ++i) { //节点还没访问,创建新节点 if (visit.find(temp1->neighbors[i]->label) == visit.end()) { UndirectedGraphNode * temp = new UndirectedGraphNode((temp1->neighbors)[i]->label); (temp2->neighbors).push_back(temp); initial_queue.push((temp1->neighbors)[i]); visit.insert((temp1->neighbors)[i]->label); result_queue.push(temp); copy_list.push_back(temp); } else { //遍历链表,使得访问节点指针指向链表中该节点。 for (list<UndirectedGraphNode *>::const_iterator iter = copy_list.begin(); iter != copy_list.end(); iter++) { if ((*iter)->label == temp1->neighbors[i]->label) { (temp2->neighbors).push_back((*iter)); } } } } } return result; } };
难点解析:
- 每次遍历队列中的第一个元素,将其指向的元素new后添加到队列中,但是当队列中已有该元素时,我们仅仅需要创建一个指针指向已有的元素就好。此时需要遍历整个队列才能得到该指针信息。
- 由于我们在后续操作中是需要队列信息的,所以我们没有办法遍历队列,因为遍历完之后队列就被清空了。所以无奈之下我们选用了另一个链表来存储队列信息。
- 要记录一个节点是否已被访问,我们可以用一个bool数组来表示,但是这里节点数量是不确定的。所以我们用了一个容器set来存储已经访问过的节点。
LeetCode上更简洁的解题方法:
class Solution {
public:
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> hash;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) return node;
if(hash.find(node) == hash.end()) {
hash[node] = new UndirectedGraphNode(node -> label);
for (auto x : node -> neighbors) {
(hash[node] -> neighbors).push_back( cloneGraph(x) );
}
}
return hash[node];
}
};