-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clone Graph.cpp
43 lines (42 loc) · 1.5 KB
/
Clone Graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node==NULL)
{
return NULL;
}
map<UndirectedGraphNode*,UndirectedGraphNode*> bMap;
queue<UndirectedGraphNode*> que;
que.push(node);
while(!que.empty())
{
UndirectedGraphNode* pNode=que.front();//从队列取出一个结点
que.pop();
if(!bMap.count(pNode))//如果我hash map里面没有这个结点我就新建,并且插入我的hash-map
{
UndirectedGraphNode* newNode=new UndirectedGraphNode(pNode->label);
bMap[pNode]=newNode;
}
for(int i=0;i<pNode->neighbors.size();i++)//遍历刚刚这个结点的每一个邻居结点
{
UndirectedGraphNode* ChildNode=pNode->neighbors[i];
if(!bMap.count(ChildNode))
{
UndirectedGraphNode* newNode=new UndirectedGraphNode(ChildNode->label);
bMap[ChildNode]=newNode;
que.push(ChildNode);
}
bMap[pNode]->neighbors.push_back(bMap[ChildNode]);//让它的所有邻居都放在它的邻居链表中
}
}
return bMap[node];//这个是头结点
}
};