LeetCode题解之Clone Graph

1、题目描述

2、问题分析

要遍历图,然后标记没有被复制的节点。

3、代码

 1 class Solution {
 2
 3 private:
 4     unordered_map<Node*, Node*> m;
 5
 6 public:
 7     Node* cloneGraph(Node* node) {
 8         if (node == NULL)
 9             return NULL;
10
11         Node *copy = new Node(node->val, {});
12         queue<Node*> q;
13         m[node] = copy;
14         q.push(node);
15
16         while (!q.empty()) {
17             Node *cur = q.front();
18             q.pop();
19
20             for(Node* neighbor : cur->neighbors) {
21                 if (m.find(neighbor) == m.end()) {
22                    m[neighbor] = new Node(neighbor->val, {});
23                     q.push(neighbor);
24                 }
25                 m[cur]->neighbors.push_back(m[neighbor]);
26
27             }
28         }
29         return copy;
30     }
31 };

原文地址:https://www.cnblogs.com/wangxiaoyong/p/10472597.html

时间: 2024-08-06 00:16:23

LeetCode题解之Clone Graph的相关文章

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

leetcode || 133、Clone Graph

problem: 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 e

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

【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 [133]

[题目] 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

leetcode -day11 Clone Graph &amp; Palindrome Partitioning I II

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

【LeetCode】Clone Graph (2 solutions)

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