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

  

 struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
}; //DFS
class Solution01 {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
return helper(node, mapR);
}
UndirectedGraphNode* helper(UndirectedGraphNode*node, unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>&mapR)
{
if (node == nullptr)return nullptr;
if (mapR.find(node) != mapR.end())//旧节点
return mapR[node];
UndirectedGraphNode* res = new UndirectedGraphNode(node->label);//新节点
mapR[node] = res;
for (auto a : node->neighbors)
res->neighbors.push_back(helper(a, mapR));
return res;
}
};
//BFS
class Solution02 {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
queue<UndirectedGraphNode*>qNode;
qNode.push(node);
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
mapR[node] = res;
while (!qNode.empty())
{
UndirectedGraphNode *p = qNode.front();
qNode.pop();
for (auto a : p->neighbors)
{
if (mapR.find(a) == mapR.end())//新节点
{
qNode.push(a);//加入
mapR[a] = new UndirectedGraphNode(a->label);//新节点
}
mapR[p]->neighbors.push_back(mapR[a]);//连接节点
}
}
return res;
}
};

最新文章

  1. sql语句查询经纬度范围(转载,源链接失效)
  2. 阴影 box-shadow
  3. golang: 根据json生成go源文件
  4. js 遮罩层 loading 效果
  5. HDU 4974 Dracula and Ethan 优先队列
  6. 移动APP服务端API设计应该考虑到的问题
  7. jquery-multiselect在ie6里的一个bug
  8. JVM的生命周期——JVM之二
  9. windows composer安装
  10. Java 9 揭秘(13. Collection API 更新)
  11. 学习js函数--函数定义
  12. NYoj_49开心的小明
  13. 关于MarkDown里的图片问题
  14. Jmeter学习之--dubbo接口测试
  15. SpringBoot系列——快速构建项目
  16. 「TJOI2015」线性代数 解题报告
  17. [Big Data - ZooKeeper] ZooKeeper: A Distributed Coordination Service for Distributed Applications
  18. 第三百九十六节,Django+Xadmin打造上线标准的在线教育平台—其他插件使用说,自定义列表页上传插件
  19. vue 脚手架搭建新项目以及element-ui等vue组件的使用
  20. neo4j CQL 使用

热门文章

  1. iText例子
  2. [USACO 07NOV]电话线Telephone Wire
  3. [题解]Print a 1337-string...-数学(codeforces 1202D)
  4. 嵌入式C语言3.4 关键字---类型描述符auto/register/static/const/extern/volatile/
  5. IIS 解决跨域问题
  6. 电脑同时安装python2和python3 ,默认使用python3
  7. undefined,null,var 0 = {},var s = &#39;&#39;,的区别
  8. APACHE两种域名跳转法简单完成重定向
  9. spring-data-neo4j了解
  10. vue - blog开发学习6