Clone graph
Title description: Give you a reference to a node in an undirected connected graph. Please return a deep copy (clone) of the graph.
Each node in the graph contains its value val (int) and a list of its neighbors (list[Node]).
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/clone-graph/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Depth-first traversal
First, if the current node is empty, it does not need to be processed and returns directly.
Otherwise, use a Map that is visited to store the processed graph nodes and the corresponding clone nodes. The recursive process is as follows:
- Judge whether the current node is in visited, if it is, it means that it has been processed, then directly take the clone of the current node from visited and return.
- Otherwise, a new cloned node is cloned according to the current node, and the neighbor nodes of the newly cloned node are initialized to be empty, and then the current node and the new cloned node are added to visited;
- Then recursively process the neighbor nodes of the current node.
Finally, the clone node of the current node is returned.
Solution 2: Breadth first traversal
Similarly, first, if the current node is empty, it does not need to be processed and returns directly.
Otherwise, it is also necessary to initialize a Map that is visited to store the processed graph nodes and the corresponding cloned nodes. First, add the current node and the nodes cloned by the current node to visited, and then add the current node to a queue. Then process the nodes in the queue, knowing that the queue is not empty, the processing process is as follows:
- Take out the head node of the queue as curNode;
- Traverse the neighbor nodes of the curNode node;
- If the current neighbor node is not visited, add it and the corresponding clone node to visited and add it to the queue;
- Then add the clone node of the current neighbor node to the neighbor node list of the clone node of curNode.
Finally, return to the clone node of the original node.
Note : I don't know much about graph-related algorithms, so I have to learn more.
import com.kaesar.leetcode.GraphNode;
import java.util.*;
public class LeetCode_133 {
private static Map<GraphNode, GraphNode> visited = new HashMap<>();
/**
* 深度优先遍历
*
* @param node 当前节点
* @return
*/
public static GraphNode cloneGraph(GraphNode node) {
if (node == null) {
return node;
}
// 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
if (visited.containsKey(node)) {
return visited.get(node);
}
// 克隆当前节点
GraphNode cloneNode = new GraphNode(node.val, new ArrayList<>());
// 哈希表存储
visited.put(node, cloneNode);
// 递归处理当前节点的邻居节点
for (GraphNode neighbor : node.neighbors) {
cloneNode.neighbors.add(cloneGraph(neighbor));
}
return cloneNode;
}
/**
* 广度优先遍历
*
* @param node 当前节点
* @return
*/
public static GraphNode cloneGraph2(GraphNode node) {
if (node == null) {
return node;
}
HashMap<GraphNode, GraphNode> visited = new HashMap<>();
Queue<GraphNode> queue = new LinkedList<>();
// 将当前节点添加到队列中
queue.add(node);
// 克隆第一个节点并存储到哈希表中
visited.put(node, new GraphNode(node.val, new ArrayList<>()));
// 广度优先遍历
while (!queue.isEmpty()) {
// 取出队列的头节点
GraphNode curNode = queue.remove();
// 遍历当前节点的邻居节点
for (GraphNode neighbor : curNode.neighbors) {
if (!visited.containsKey(neighbor)) {
visited.put(neighbor, new GraphNode(neighbor.val, new ArrayList<>()));
// 将邻居节点添加到队列中
queue.add(neighbor);
}
// 更新当前节点的邻居节点列表
visited.get(curNode).neighbors.add(visited.get(neighbor));
}
}
return visited.get(node);
}
public static void main(String[] args) {
}
}
[Daily Message] Do it yourself, you will have plenty of food and clothing!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。