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. js固定表头的实现(转)

    原文链接:http://www.th7.cn/web/js/201509/121055.shtml 参考链接:http://www.jb51.net/article/102568.htm 写两个表格, ...

  2. linux提权辅助工具(一):linux-exploit-suggester.sh

    来自:https://raw.githubusercontent.com/mzet-/linux-exploit-suggester/master/linux-exploit-suggester.sh ...

  3. C++ 4 种具有更 为准确语义的新强制转换类型

    1. static_cast<T>() 可用于把指向A 的指针强制转换为指向B 的指针,其约束条件是类B必须是类A的子类.例如:A *obj = new B;B *b = static_c ...

  4. SocketServer模块 《Python核心编程(第3版)》——2.5

    本文内容参考文章地址: https://m.aliyun.com/yunqi/articles/93088/ SocketServer模块 SocketServer是标准库中的一个高级模块(Pytho ...

  5. 基于zookeeper简单实现分布式锁

    https://blog.csdn.net/desilting/article/details/41280869 这里利用zookeeper的EPHEMERAL_SEQUENTIAL类型节点及watc ...

  6. LOJ10131. 「一本通 4.4 例 2」暗的连锁【树上差分】

    LINK solution 很简单的题 你就考虑实际上是对每一个边求出两端节点分别在两个子树里面的附加边的数量 然后这个值是0第二次随便切有m种方案,如果这个值是1第二次只有一种方案 如果这个值是2或 ...

  7. java中读取配置文件

    若是Javaweb项目,项目运行于tomcat或其他容器时,可以使用下面方式来获取文件的输入流 1.当属性文件放在src下面时 InputStream is = Thread.currentThrea ...

  8. centos中如何安装php-bcmath扩展?

    talk is cheap,show me the code: [root@LAMP1 lib]# php -v PHP (cli) (built: Oct ::) Copyright (c) - T ...

  9. UDP的connect

    UDP的connect没有三次握手过程,内核只是检测是否存在立即可知的错误(如一个显然不可达的目的地), 记录对端的的IP地址和端口号,然后立即返回调用进程. 未连接UDP套接字(unconnecte ...

  10. C#操作 iis启用父目录

    iis6实现: DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer&quo ...