Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the given node and each of the nodes in its neighbors.

OJ's undirected graph serialization (so you can understand error output):

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 #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. 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
/ \
\_/

Note: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. You don't need to understand the serialization to solve the problem.

DFS

 /**
* 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;
if (mp.find(node->label) == mp.end()){
mp[node->label] = new UndirectedGraphNode(node -> label);
for (UndirectedGraphNode* neigh : node -> neighbors)
mp[node->label] -> neighbors.push_back(cloneGraph(neigh));
}
return mp[node->label]; }
private:
unordered_map<int, UndirectedGraphNode*> mp;
};

133. Clone Graph(图的复制)的更多相关文章

  1. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  2. 133. Clone Graph (3 solutions)——无向无环图复制

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  3. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  4. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  5. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  6. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. 【LeetCode】133. Clone Graph 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. 133. Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  9. 133. Clone Graph (Graph, Map; DFS)

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

随机推荐

  1. 用WordPress建立专业网站教程 (一步步建站, 一步也不少)

    最新美国域名中心US Domain Center: http://www.usdomaincenter.com/ 建站教程 (10分钟上线, 无需备案): https://www.qiyewp.com ...

  2. java把一个list分割成多个list存入map中(实例)

    这都是最近我写工具遇到的一些点, 这些点就是指我在网上没搜到答案,然后实际上我为此花费了时间的 public static void main(String[] args) { List<Str ...

  3. Deseq2 的可视化策略汇总

    1) MA图   对于MA图而言, 横坐标为该基因在所有样本中的均值,basemean = (basemean_A + basemean_B ) / 2, 纵坐标为 log2Fold change 其 ...

  4. 3D游戏图形引擎

    转自:http://www.cnblogs.com/live41/archive/2013/05/11/3072282.html CryEngine 3  http://www.crydev.net/ ...

  5. 我们正在招聘java工程师,想来美团工作吗?

    我们希望你有? 1.3年以上Java服务器开发经验,精通Java及面向对象设计开发,熟悉主流web框架 2.熟悉网络编程,熟悉TCP/IP协议,熟悉互联网应用协议 3.有大规模分布式系统设计与开发经验 ...

  6. go与c语言的互操作

    https://tonybai.com/2012/09/26/interoperability-between-go-and-c/ https://tonybai.com/2016/02/21/som ...

  7. axios设置application/x-www-form-urlencoded

    this.$axios({ method: 'post', url: 'http://www.17huo.com/tusou/deeplorSearch.html', headers: { 'Cont ...

  8. JVM监控和调优常用命令工具总结

    JVM监控和调优 在Java应用和服务出现莫名的卡顿.CPU飙升等问题时总是要分析一下对应进程的JVM状态以定位问题和解决问题并作出相应的优化,在这过程中Java自带的一些状态监控命令和图形化工具就非 ...

  9. An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later.

    刚装完 PHP.Nginx,准备跑下 phpMyAdmin 程序,结果报以下错误: An error occurred. Sorry, the page you are looking for is ...

  10. 使用PsExec获取shell执行命令

    PsExec PsExec是pstools工具组套件的一部分,确成为了渗透利器,下载地址:点击这里下载 连接shell 我的Windows Server 2012默认打开域网络防火墙的时候,是不能连接 ...