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. windows上安装maven及eclipse中配置maven

    本地安装与配置: 1.jdk 在cmd中运行 Java -version 2.下载maven包 https://maven.apache.org/download.cgi下载最新版的Maven程序:( ...

  2. 本地代码同步到github

    1 设置 ssh 公钥信息 首先你要确保 github 账号设置了ssh 公钥信息.如果没有的话可以按照下面的方式设置: 前往 github 网站的 account settings, 依次点击 Se ...

  3. I.MX6 U-boot lvds display hacking

    /*********************************************************************************** * I.MX6 U-boot ...

  4. BZOJ2186: [Sdoi2008]沙拉公主的困惑(求[1,N!]与M!互素的个数)(线性筛)

    2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 6103  Solved: 2060[Submit][S ...

  5. Testng优势

    选择Testng的理由: 1.可指定执行顺序, dependsOnMethods 属性来应对测试的依赖性问题. 2.·参数化1:轻轻松松从XML中得到参数 @BeforeClass public vo ...

  6. 写写Django中DRF框架概述以及序列化器对象serializer的构造方法以及使用

    写写Django中DRF框架概述以及序列化器对象serializer的构造方法以及使用 一.了解什么是DRF DRF: Django REST framework Django REST framew ...

  7. jsp中9个隐含对象

    在JSP中一共有9个隐含对象,这个9个对象我可以在JSP中直接使用.因为在service方法已经对这个九个隐含对象进行声明及赋值,所以可以在JSP中直接使用. - pageContext 类型:Pag ...

  8. Flask 模板语法

    Flask中默认的模板语言是Jinja2 STUDENT = {'name': 'Old', 'age': 38, 'gender': '中'}, STUDENT_LIST = [ {'name': ...

  9. struts2学习(2)struts2核心知识

    一.Struts2 get/set 自动获取/设置数据 根据上一章.中的源码继续. HelloWorldAction.java中private String name,自动获取/设置name: pac ...

  10. 使用libssh2连接到远程服务器

    libssh2-1.7.0.tar.gz  示例代码:libssh2-1.7.0.tar.gz\libssh2-1.7.0\example 官网示例 https://www.libssh2.org/e ...