|
11 | 11 |
|
12 | 12 | ## [133. Clone Graph (Medium)](https://leetcode.com/problems/clone-graph "克隆图")
|
13 | 13 |
|
14 |
| -<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, return a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><strong>deep copy</strong></a> (clone) of the graph. Each node in the graph contains a val (<code>int</code>) and a list (<code>List[Node]</code>) of its neighbors.</p> |
| 14 | +<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> |
| 15 | + |
| 16 | +<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> |
| 17 | + |
| 18 | +<p>Each node in the graph contains a val (<code>int</code>) and a list (<code>List[Node]</code>) of its neighbors.</p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +class Node { |
| 22 | + public int val; |
| 23 | + public List<Node> neighbors; |
| 24 | +} |
| 25 | +</pre> |
15 | 26 |
|
16 | 27 | <p> </p>
|
17 | 28 |
|
18 |
| -<p><strong>Example:</strong></p> |
| 29 | +<p><strong>Test case format:</strong></p> |
19 | 30 |
|
20 |
| -<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" style="width: 500px;height:550px" /></p> |
| 31 | +<p>For simplicity sake, 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> |
21 | 32 |
|
| 33 | +<p><b>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> |
| 34 | + |
| 35 | +<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> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong>Example 1:</strong></p> |
| 39 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" style="width: 500px; height: 550px;" /> |
22 | 40 | <pre>
|
23 |
| -<strong>Input: |
24 |
| -</strong>{"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1} |
25 |
| - |
26 |
| -<strong>Explanation:</strong> |
27 |
| -Node 1's value is 1, and it has two neighbors: Node 2 and 4. |
28 |
| -Node 2's value is 2, and it has two neighbors: Node 1 and 3. |
29 |
| -Node 3's value is 3, and it has two neighbors: Node 2 and 4. |
30 |
| -Node 4's value is 4, and it has two neighbors: Node 1 and 3. |
| 41 | +<strong>Input:</strong> adjList = [[2,4],[1,3],[2,4],[1,3]] |
| 42 | +<strong>Output:</strong> [[2,4],[1,3],[2,4],[1,3]] |
| 43 | +<strong>Explanation:</strong> There are 4 nodes in the graph. |
| 44 | +1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). |
| 45 | +2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). |
| 46 | +3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). |
| 47 | +4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). |
31 | 48 | </pre>
|
32 | 49 |
|
33 |
| -<p> </p> |
| 50 | +<p><strong>Example 2:</strong></p> |
| 51 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/01/07/graph.png" style="width: 163px; height: 148px;" /> |
| 52 | +<pre> |
| 53 | +<strong>Input:</strong> adjList = [[]] |
| 54 | +<strong>Output:</strong> [[]] |
| 55 | +<strong>Explanation:</strong> Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors. |
| 56 | +</pre> |
| 57 | + |
| 58 | +<p><strong>Example 3:</strong></p> |
| 59 | + |
| 60 | +<pre> |
| 61 | +<strong>Input:</strong> adjList = [] |
| 62 | +<strong>Output:</strong> [] |
| 63 | +<strong>Explanation:</strong> This an empty graph, it does not have any nodes. |
| 64 | +</pre> |
34 | 65 |
|
35 |
| -<p><strong>Note:</strong></p> |
| 66 | +<p><strong>Example 4:</strong></p> |
| 67 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/01/07/graph-1.png" style="width: 272px; height: 133px;" /> |
| 68 | +<pre> |
| 69 | +<strong>Input:</strong> adjList = [[2],[1]] |
| 70 | +<strong>Output:</strong> [[2],[1]] |
| 71 | +</pre> |
| 72 | + |
| 73 | +<p> </p> |
| 74 | +<p><strong>Constraints:</strong></p> |
36 | 75 |
|
37 |
| -<ol> |
38 |
| - <li>The number of nodes will be between 1 and 100.</li> |
39 |
| - <li>The undirected graph is a <a href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)#Simple_graph" target="_blank">simple graph</a>, which means no repeated edges and no self-loops in the graph.</li> |
40 |
| - <li>Since the graph is undirected, if node <em>p</em> has node <em>q</em> as neighbor, then node <em>q</em> must have node <em>p</em> as neighbor too.</li> |
41 |
| - <li>You must return the <strong>copy of the given node</strong> as a reference to the cloned graph.</li> |
42 |
| -</ol> |
| 76 | +<ul> |
| 77 | + <li><code>1 <= Node.val <= 100</code></li> |
| 78 | + <li><code>Node.val</code> is unique for each node.</li> |
| 79 | + <li>Number of Nodes will not exceed 100.</li> |
| 80 | + <li>There is no repeated edges and no self-loops in the graph.</li> |
| 81 | + <li>The Graph is connected and all nodes can be visited starting from the given node.</li> |
| 82 | +</ul> |
43 | 83 |
|
44 | 84 | ### Related Topics
|
45 | 85 | [[Depth-first Search](../../tag/depth-first-search/README.md)]
|
|
0 commit comments