题目:
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 #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
题目分析:题目要求深度遍历一个无向图。图中每一个节点都有一个唯一的label,然后可以有很多的neighbors,其中neighbor也可以是自己。
思路:
采用深度遍历图的方式,一个个节点的复制。为了避免重复复制节点,创建一个HashMap,记录已经创建过的节点。(刚开始使用了原节点node的label做key,所复制节点copy做value,即<node.label,copy>。在LeetCode上总是提示WrongAnswer。但在eclipse上运行同样的输入,能够得到正确的答案。难道是因为版本不同?【LeetCode上是1.7,PC上是1.6】。后来改用<node,copy>作map,就能通过了)
具体算法如下:
- 创建一个HashMap记录已经复制过的节点
- 从节点node开始,深度遍历图。创建一个copy节点,复制node的label。将<node,copy>放入map中。
- 遍历node的neighbors。
- 如果一个neighbor已在map中,说明其在之前已经被复制。直接将map中对应的节点放入copy的neighbors列表中。
- 如果一个neighbor不再map中,递归调用cloneGraph来创建节点,并且加入到copy的neighbors列表中
- 返回copy
注意点:
- 刚开始,创建map使用了<node.label,copy>对。因为对于node,其label也是唯一的。应该没有问题,在eclipse上运行也能给出正确答案。但在LeetCode上,相同输入,却得到一个错误答案。可能是版本不同引起的。也可能是LeetCode本身的问题。后来,其他代码未变,map改用<node,copy>对,LeetCode上也能成功通过。
- 例如,输入{0,1#1,2#2,2} , LeetCode上输出是{0,1#1} ,而eclipse上能够得到{0,1#1,2#2,2}这种结果。
代码:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
import java.util.HashMap;
public class Solution {
static HashMap<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null){
return null;
}
UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
map.put(node,copy);
for (int i = 0; i < node.neighbors.size(); i++){
UndirectedGraphNode thisNeighbor = node.neighbors.get(i);
if(map.containsKey(thisNeighbor)){
copy.neighbors.add(map.get(thisNeighbor));
}else{
copy.neighbors.add(cloneGraph(thisNeighbor));
}
}
return copy;
}
}
未通过的代码:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
import java.util.HashMap;
public class Solution {
static HashMap<Integer,UndirectedGraphNode> map = new HashMap<Integer,UndirectedGraphNode>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null){
return null;
}
UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
map.put(copy.label,copy);
for (int i = 0; i < node.neighbors.size(); i++){
UndirectedGraphNode thisNeighbor = node.neighbors.get(i);
if(map.containsKey(thisNeighbor.label)){
copy.neighbors.add(map.get(thisNeighbor.label));
}else{
copy.neighbors.add(cloneGraph(thisNeighbor));
}
}
return copy;
}
}