Problem Clone Graph

Problem Description:

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

Solution:

 1     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
 2         if (node == null) {
 3             return null;
 4         }
 5         Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
 6         Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
 7         queue.add(node);
 8         while (!queue.isEmpty()) {
 9             UndirectedGraphNode q = queue.poll();
10             UndirectedGraphNode newNode = new UndirectedGraphNode(q.label);
11             map.put(q, newNode);
12             for (UndirectedGraphNode neighbor : q.neighbors) {
13                 if (!map.containsKey(neighbor)) {
14                     queue.add(neighbor);
15                 }
16             }
17         }
18         Set<UndirectedGraphNode> keySet = map.keySet();
19         for (UndirectedGraphNode p : keySet) {
20             UndirectedGraphNode n  = map.get(p);
21             for (UndirectedGraphNode q : p.neighbors) {
22                 n.neighbors.add(map.get(q));
23             }
24         }
25
26         return map.get(node);
27     }

Problem Clone Graph

时间: 2024-10-11 22:28:27

Problem Clone Graph的相关文章

21. Clone Graph

Clone Graph 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 an

[leecode]Clone Graph

Clone Graph 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 an

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

leetcode题目:Clone Graph

题目: 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 n

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 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 lab

[Leetcode][JAVA] Clone Graph, Copy List with Random Pointer

Clone Graph: 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 a

[LintCode] Clone Graph

Clone Graph 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 an

Clone Graph leetcode java(DFS and BFS 基础)

题目: 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 n

【leetcode】Clone Graph

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 neigh