leetcode:Clone Graph
2016-02-13 18:20
447 查看
Clone an undirected graph. Each node in the graph contains a
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use
a separator for node label and each neighbor of the node.
As an example, consider the serialized graph
The graph has a total of three nodes, and therefore contains three parts as separated by
First node is labeled as
both nodes
Second node is labeled as
node
Third node is labeled as
node
Visually, the graph looks like the following:
Subscribe to see which companies asked this question
BFS solution:
DFS solution https://leetcode.com/discuss/44332/java-dfs-solution-recursion public
UndirectedGraphNode
cloneGraph(UndirectedGraphNode
root)
{ if
(root
== null)
return
null;
//
use
a
map
to
save
cloned
nodes
Map<UndirectedGraphNode,
UndirectedGraphNode>
map
= new
HashMap<UndirectedGraphNode,
UndirectedGraphNode>();
//
clone
the
root
map.put(root,
new
UndirectedGraphNode(root.label));
helper(root,
map);
return
map.get(root);}void
helper(UndirectedGraphNode
root,
Map<UndirectedGraphNode,
UndirectedGraphNode>
map)
{ for
(UndirectedGraphNode
neighbor
:
root.neighbors)
{ if
(!map.containsKey(neighbor))
{ map.put(neighbor,
new
UndirectedGraphNode(neighbor.label));
helper(neighbor,
map);
} map.get(root).neighbors.add(map.get(neighbor));
}}
labeland 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
#.
First node is labeled as
0. Connect node
0to
both nodes
1and
2.
Second node is labeled as
1. Connect node
1to
node
2.
Third node is labeled as
2. Connect node
2to
node
2(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
Subscribe to see which companies asked this question
BFS solution:
/** * 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; map<int, UndirectedGraphNode*> auxMap; list<UndirectedGraphNode*> auxList; auxList.push_back(node); UndirectedGraphNode* retNode = NULL; while (auxList.size() > 0) { UndirectedGraphNode* curNode = auxList.front(); auxList.pop_front(); if (auxMap.find(curNode->label) == auxMap.end()) { auxMap[curNode->label] = new UndirectedGraphNode(curNode->label); } UndirectedGraphNode* curNewNode = auxMap[curNode->label]; if (retNode == NULL) retNode = curNewNode; for (int i=0; i<curNode->neighbors.size(); i++) { UndirectedGraphNode *curNeighbor = curNode->neighbors[i]; if (auxMap.find(curNeighbor->label) == auxMap.end()) { UndirectedGraphNode *newNeighbor = new UndirectedGraphNode(curNeighbor->label); auxMap[curNeighbor->label] = newNeighbor; auxList.push_back(curNeighbor); } curNewNode->neighbors.push_back(auxMap[curNeighbor->label]); } } return retNode; } };
DFS solution https://leetcode.com/discuss/44332/java-dfs-solution-recursion public
UndirectedGraphNode
cloneGraph(UndirectedGraphNode
root)
{ if
(root
== null)
return
null;
//
use
a
map
to
save
cloned
nodes
Map<UndirectedGraphNode,
UndirectedGraphNode>
map
= new
HashMap<UndirectedGraphNode,
UndirectedGraphNode>();
//
clone
the
root
map.put(root,
new
UndirectedGraphNode(root.label));
helper(root,
map);
return
map.get(root);}void
helper(UndirectedGraphNode
root,
Map<UndirectedGraphNode,
UndirectedGraphNode>
map)
{ for
(UndirectedGraphNode
neighbor
:
root.neighbors)
{ if
(!map.containsKey(neighbor))
{ map.put(neighbor,
new
UndirectedGraphNode(neighbor.label));
helper(neighbor,
map);
} map.get(root).neighbors.add(map.get(neighbor));
}}
相关文章推荐
- Codeforces 624B Making a String 【水题】
- 141. Linked List Cycle LeetCode
- Codeforces 624A Save Luke 【数学】
- Ubuntu 安装 JAVA 8
- 137. Single Number II LeetCode
- Mysql的大字段问题
- CMD与AMD比较
- usaco 3.4 Electric Fence 2010.8.4
- Java日志框架
- 单片机keil C中的data、bdata、idata、xdata、hdata、pdata、code解释
- 1027. 打印沙漏
- 点击<li>标签触发<a>中的超链接
- 随机数生成之Math.Random()方法
- Python ftp上传文件
- Renascence架构原理——遗传规划算法
- spoj196 2010.8.2
- 回文字符
- 打印函数调用栈
- java&javaweb学习笔记(汇总)
- Bravo!Java学习笔记(1)---接口、抽象类、对象克隆、内部类