## 133. Clone Graph 原

cofama

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.Nodes are labeled uniquely.

BFS：

``````/**
* 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<int, UndirectedGraphNode *> m;
m[node->label] = new UndirectedGraphNode(node->label);
queue<UndirectedGraphNode *> q;
q.push(node);

UndirectedGraphNode *current;

while(!q.empty()) {
current = q.front();
for(UndirectedGraphNode *x : current->neighbors) {
if(m.find(x->label)==m.end()) {
m[x->label] = new UndirectedGraphNode(x->label);
q.push(x);
}
m[current->label]->neighbors.push_back(m[x->label]);
}
q.pop();
}
return m[node->label];
}
};
``````

DFS：

``````class Solution {
public:
map<UndirectedGraphNode*, UndirectedGraphNode*> m;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node==NULL) return NULL;
if(m.count(node) == 0) {
m[node] = new UndirectedGraphNode(node->label);
for (UndirectedGraphNode* x : node->neighbors) {
m[node]->neighbors.push_back(cloneGraph(x));
}
}
return m[node];
}
};
``````

© 著作权归作者所有

### cofama

FTP服务器

2016/10/14
14
0
Bonobo Git Server 3.3 发布

Bonobo Git Server 3.3 发布，此版本更新内容如下： 新特性 Clone button for repositories in web management UI - latop2604 Support for custom title, logo, additional footer message......

oschina
2014/08/25
1K
1

2017/09/28
0
0
zabbix（二）——基本使用

step1：首次登陆zabbix #### 主选项卡： #### Administration：负责常规设置，用户验证，媒介设定相关设定 #### configuration：顾名思义，主要的配置都是通过此选项来设定 #### monitoring：...

2014/05/11
0
2
Zeppelin 0.5.5-incubating 发布，云计算管理和监控

Zeppelin 0.5.5-incubating 发布，此版本主要关注后端支持改进，稳定性改进，简化配置，有超过 60 位贡献者参与，提供新特性，改进和修复，解决了 90+ issues。 此版本后端支持： Apache Ign...

2015/11/20
751
0

0.01-Win10安装linux子系统

2
0
init 0-6 （启动级别:init 0,1,2,3,4,5,6）

2
0
Android Camera HAL浅析

1、Camera成像原理介绍 Camera工作流程图 Camera的成像原理可以简单概括如下： 景物(SCENE)通过镜头（LENS）生成的光学图像投射到图像传感器(Sensor)表面上，然后转为电信号，经过A/D（模数转...

2
0

go4it

3
0
mysql PL(procedure language)流程控制语句

edison_kwok

3
0