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

  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
/ \
\_/
思路:对neughbors的每个节点,如果还没创建,DFS
所以需要一个map标示节点是否已创建
每次递归内容为创建该节点,并递归创建邻节点。
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
}; class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node) return NULL;
UndirectedGraphNode *current;
map<UndirectedGraphNode*,UndirectedGraphNode*> flag; //前一个元素是节点在原Graph中的地址,后一个元素是节点在新拷贝的图中的位置
UndirectedGraphNode *root = cloneNode(node,flag);
return root;
} UndirectedGraphNode * cloneNode(UndirectedGraphNode *source, map<UndirectedGraphNode*,UndirectedGraphNode*> &flag)
{
if(flag.find(source)!= flag.end()) return flag[source]; //如果map中没有该节点,那么创建该节点
UndirectedGraphNode *target = new UndirectedGraphNode(source->label);
flag[source] = target; for(vector<UndirectedGraphNode *>::iterator it = source->neighbors.begin(); it < source->neighbors.end(); it++ )
{
UndirectedGraphNode *newRoot = cloneNode(*it, flag); //深度优先,先递归处理它的某一个邻居,再处理其他邻居
target->neighbors.push_back(newRoot);
}
return target; //返回已经处理好的节点
}
};

133. Clone Graph (Graph, Map; DFS)的更多相关文章

  1. Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))

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

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

  3. 1131 Subway Map DFS解法 BFS回溯!

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  4. LYK loves graph(graph)

    题目: LYK loves graph(graph) Time Limit:2000ms   Memory Limit:128MB LYK喜欢花花绿绿的图片,有一天它得到了一张彩色图片,这张图片可以看 ...

  5. leetcode 133. Clone Graph ----- java

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

  6. 133. Clone Graph

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

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

  8. Java for LeetCode 133 Clone Graph

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

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

随机推荐

  1. MAC 下编译 ANDROID P 源码 提示 internal error: Could not find a supported mac sdk: ["10.10" "10.11" "10.12" "10.13"]

    MAC 下编译 ANDROID P 源码出现下面的问题: ninja: no work to do. [21/21] out/soong/.bootstrap/bin/soong_build out/ ...

  2. ROS会议 ROSCon 2017

    ----ROSCon2012-2017----来源链接:https://roscon.ros.org           近三年ROSCon(2015-2017)都会将会议视频录像和文档公开~以下为机 ...

  3. Swift UIImageView和UISlider组合

    /***************火焰图片Demo************start*******/ var imgView: UIImageView? override func viewDidLoa ...

  4. 把字符串中的空格替换为"%20"

    这个需要注意的是字符串的结尾最后一个字符为'\0',并不是空字符,复制时要一块复制,算法思想就是先计算出字符串中总的空格数,然后 重新计算字符串的长度,由于"%20"为3个字符,比 ...

  5. Ubuntu下的计算器

    今天计算乘法时居然给算错了,好囧. 于是开始使用ubuntu自带的计算器:gcalctool 使用方法: 在终端直接输入命令gcalctool即可.

  6. 20155317 2016-2017-2 《Java程序设计》第8周学习总结

    20155317 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 1.java.util.logging包提供了日志功能相关类与接口. 2.使用日志的起点是L ...

  7. 【转】Ubuntu13.04配置:Vim+Syntastic+Vundle+YouCompleteMe

    原文网址:http://www.cnblogs.com/csuftzzk/p/3435710.html 序言 使用Ubuntu和vim已经有一段时间了,对于Vim下的插件应用,我总是抱着一股狂热的态度 ...

  8. hadoop之 Hadoop 2.x HA 、Federation

    HDFS2.0之HA 主备NameNode: 1.主NameNode对外提供服务,备NameNode同步主NameNode元数据,以待切换: 2.主NameNode的信息发生变化后,会将信息写到共享数 ...

  9. ProcessHelp 进程类(启动,杀掉,查找)

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  10. ivy antlib shemalocation

    <?xml version="1.0" encoding="UTF-8"?> <project name="yourproject& ...