原题地址

方法I,DFS

一边遍历一边复制

借助辅助map保存已经复制好了的节点

对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟。

代码:

 map<UndirectedGraphNode *, UndirectedGraphNode *> old2new;

 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node)
return NULL;
if (old2new.find(node) != old2new.end())
return old2new[node]; UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(node, newNode));
for (auto n : node->neighbors)
newNode->neighbors.push_back(cloneGraph(n));
return newNode;
}

方法II,BFS

一边遍历一边复制

同样借助一个辅助map保存复制过的节点,还需要一个set保存已经遍历过的节点

代码:

 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node)
return NULL; map<UndirectedGraphNode *, UndirectedGraphNode *> old2new;
unordered_set<UndirectedGraphNode *> copied;
queue<UndirectedGraphNode *> que; old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(node, new UndirectedGraphNode(node->label)));
que.push(node);
while (!que.empty()) {
UndirectedGraphNode *front = que.front();
que.pop();
if (copied.find(front) != copied.end())
continue;
for (auto n : front->neighbors) {
if (old2new.find(n) == old2new.end()) {
UndirectedGraphNode *newNode = new UndirectedGraphNode(n->label);
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(n, newNode));
que.push(n);
}
old2new[front]->neighbors.push_back(old2new[n]);
}
copied.insert(front);
} return old2new[node];
}

Leetcode#133 Clone Graph的更多相关文章

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

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

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

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

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

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

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

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

  7. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

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

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

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

随机推荐

  1. sqoop导入数据到hive---2

    1.hive-table 从mysql导入数据到hive表中,可以使用--hive-table来指定hive的表名,不指定hive表名,则hive表名与mysql表名保持一致. sqoop impor ...

  2. php __clone需要注意的问题

      当一个对象的属性是另外一个对象时,当有一个对象复制该对象时,当复制到这个属性(一个对象)时,只复制这个属性(对象)的引用,而不复制引用的对象. class Account{ public $bal ...

  3. Lambda Grinding Miller From Zenith

    data = """ The Basic Things About Grinding Mill A grinding mill is a unit operation d ...

  4. HTML5 的新的表单属性

    本章讲解涉及 <form> 和 <input> 元素的新属性. 新的 form 属性: autocomplete novalidate 新的 input 属性: autocom ...

  5. 一月份实现Adb小程序

    As Brian said: According to a post on xda-developers, you can enable ADB over WiFi from the device w ...

  6. C#使用SQL存储过程完整流程

    存储过程就是固化在SQL数据库系统内部的SQL语句,这样做的好处是可以提高执行效率.提高数据库的安全性.减少网络流量.接下来就讲解如何在数据库中建立一个存储过程. 打开SQL2055数据库,展开“数据 ...

  7. jQuery toggle方法的一个奇怪表现。

    function buildTree() { //$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title' ...

  8. DTAP street

    一个网站程序的上线一般要经过开发[Development]测试[Testing]验收[Acceptance]生产[Production].所以又叫做DTAP street.对应有开发环境.测试环境.验 ...

  9. Huffman树的编码译码

    上个学期做的课程设计,关于Huffman树的编码译码. 要求: 输入Huffman树各个叶结点的字符和权值,建立Huffman树并执行编码操作 输入一行仅由01组成的电文字符串,根据建立的Huffma ...

  10. PAT乙级真题1003. 我要通过!(20)(解题)

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...