Skip to content

Commit

Permalink
133
Browse files Browse the repository at this point in the history
  • Loading branch information
lzl124631x committed Feb 23, 2022
1 parent 095e654 commit d323abc
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions leetcode/133. Clone Graph/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# [133. Clone Graph (Medium)](https://leetcode.com/problems/clone-graph/)

<p>Given a reference of a node in a&nbsp;<strong><a href="https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph" target="_blank">connected</a></strong>&nbsp;undirected graph.</p>
<p>Given a reference of a node in a <strong><a href="https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph" target="_blank">connected</a></strong> undirected graph.</p>

<p>Return a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><strong>deep copy</strong></a> (clone) of the graph.</p>

<p>Each node in the graph contains a val (<code>int</code>) and a list (<code>List[Node]</code>) of its neighbors.</p>
<p>Each node in the graph contains a value (<code>int</code>) and a list (<code>List[Node]</code>) of its neighbors.</p>

<pre>class Node {
public int val;
Expand All @@ -16,15 +16,15 @@

<p><strong>Test case format:</strong></p>

<p>For simplicity sake, each&nbsp;node's value is the same as the node's index (1-indexed). For example, the first node with&nbsp;<code>val = 1</code>, the second node with <code>val = 2</code>, and so on.&nbsp;The graph is represented in the test case using an adjacency list.</p>
<p>For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with <code>val == 1</code>, the second node with <code>val == 2</code>, and so on. The graph is represented in the test case using an adjacency list.</p>

<p><b>Adjacency list</b>&nbsp;is a collection of unordered&nbsp;<b>lists</b>&nbsp;used to represent a finite graph. Each&nbsp;list&nbsp;describes the set of neighbors of a node in the graph.</p>
<p><b>An adjacency list</b> is a collection of unordered <b>lists</b> used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.</p>

<p>The given node will&nbsp;always be the first node&nbsp;with&nbsp;<code>val = 1</code>. You must return the <strong>copy of the given node</strong> as a reference to the cloned graph.</p>
<p>The given node will always be the first node with <code>val = 1</code>. You must return the <strong>copy of the given node</strong> as a reference to the cloned graph.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" style="width: 500px; height: 550px;">
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" style="width: 454px; height: 500px;">
<pre><strong>Input:</strong> adjList = [[2,4],[1,3],[2,4],[1,3]]
<strong>Output:</strong> [[2,4],[1,3],[2,4],[1,3]]
<strong>Explanation:</strong> There are 4 nodes in the graph.
Expand All @@ -48,26 +48,23 @@
<strong>Explanation:</strong> This an empty graph, it does not have any nodes.
</pre>

<p><strong>Example 4:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/07/graph-1.png" style="width: 272px; height: 133px;">
<pre><strong>Input:</strong> adjList = [[2],[1]]
<strong>Output:</strong> [[2],[1]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in the graph is in the range <code>[0, 100]</code>.</li>
<li><code>1 &lt;= Node.val &lt;= 100</code></li>
<li><code>Node.val</code> is unique for each node.</li>
<li>Number of Nodes will not exceed 100.</li>
<li>There is no repeated edges and no self-loops in the graph.</li>
<li>There are no repeated edges and no self-loops in the graph.</li>
<li>The Graph is connected and all nodes can be visited starting from the given node.</li>
</ul>


**Companies**:
[Facebook](https://leetcode.com/company/facebook), [Amazon](https://leetcode.com/company/amazon), [Microsoft](https://leetcode.com/company/microsoft), [Google](https://leetcode.com/company/google), [Bloomberg](https://leetcode.com/company/bloomberg), [Salesforce](https://leetcode.com/company/salesforce), [Twitter](https://leetcode.com/company/twitter), [Oracle](https://leetcode.com/company/oracle)

**Related Topics**:
[Depth-first Search](https://leetcode.com/tag/depth-first-search/), [Breadth-first Search](https://leetcode.com/tag/breadth-first-search/), [Graph](https://leetcode.com/tag/graph/)
[Hash Table](https://leetcode.com/tag/hash-table/), [Depth-First Search](https://leetcode.com/tag/depth-first-search/), [Breadth-First Search](https://leetcode.com/tag/breadth-first-search/), [Graph](https://leetcode.com/tag/graph/)

**Similar Questions**:
* [Copy List with Random Pointer (Medium)](https://leetcode.com/problems/copy-list-with-random-pointer/)
Expand All @@ -87,13 +84,12 @@ class Solution {
unordered_map<Node*, Node*> m;
public:
Node* cloneGraph(Node* node) {
if (!node) return NULL;
if (m.count(node) == 0) {
auto copy = new Node(node->val);
m[node] = copy;
for (auto nei : node->neighbors) copy->neighbors.push_back(cloneGraph(nei));
}
return m[node];
if (!node) return nullptr;
if (m.count(node)) return m[node];
auto cpy = new Node(node->val);
m[node] = cpy;
for (auto &n : node->neighbors) cpy->neighbors.push_back(cloneGraph(n));
return cpy;
}
};
```
Expand All @@ -109,7 +105,7 @@ public:
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return NULL;
if (!node) return nullptr;
queue<Node*> q;
unordered_map<Node*, Node*> m;
m[node] = new Node(node->val);
Expand Down

0 comments on commit d323abc

Please sign in to comment.