|
11 | 11 |
|
12 | 12 | ## 133. Clone Graph (Medium)
|
13 | 13 |
|
14 | | -<p>Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a <code>label</code> (<code>int</code>) and a list (<code>List[UndirectedGraphNode]</code>) of its <code>neighbors</code>. There is an edge between the given node and each of the nodes in its neighbors.</p> |
15 | | - |
16 | | -<div><br /> |
17 | | -<b>OJ's undirected graph serialization (so you can understand error output):</b> |
18 | | - |
19 | | -<p>Nodes are labeled uniquely.</p> |
20 | | -We use <code>#</code> as a separator for each node, and <code>,</code> as a separator for node label and each neighbor of the node. |
| 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> |
21 | 15 |
|
22 | 16 | <p> </p>
|
23 | 17 |
|
24 | | -<p>As an example, consider the serialized graph <code><fontcolor="red">{<fontcolor="black">0</font>,1,2#</font><fontcolor="blue"><fontcolor="black">1</font>,2#</font><fontcolor="green"><fontcolor="black">2</font>,2}</font></code>.</p> |
| 18 | +<p><strong>Example:</strong></p> |
25 | 19 |
|
26 | | -<p>The graph has a total of three nodes, and therefore contains three parts as separated by <code>#</code>.</p> |
| 20 | +<p><imgalt=""src="https://assets.leetcode.com/uploads/2019/02/19/113_sample.png"style="width: 200px; height: 149px;" /></p> |
27 | 21 |
|
28 | | -<ol> |
29 | | - <li>First node is labeled as <code><font color="black">0</font></code>. Connect node <code><font color="black">0</font></code> to both nodes <code><font color="red">1</font></code> and <code><font color="red">2</font></code>.</li> |
30 | | - <li>Second node is labeled as <code><font color="black">1</font></code>. Connect node <code><font color="black">1</font></code> to node <code><font color="blue">2</font></code>.</li> |
31 | | - <li>Third node is labeled as <code><font color="black">2</font></code>. Connect node <code><font color="black">2</font></code> to node <code><font color="green">2</font></code> (itself), thus forming a self-cycle.</li> |
32 | | -</ol> |
| 22 | +<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. |
| 31 | +</pre> |
33 | 32 |
|
34 | 33 | <p> </p>
|
35 | 34 |
|
36 | | -<p>Visually, the graph looks like the following:</p> |
| 35 | +<p><strong>Note:</strong></p> |
37 | 36 |
|
38 | | -<pre> |
39 | | - 1 |
40 | | - / \ |
41 | | - / \ |
42 | | - 0 --- 2 |
43 | | - / \ |
44 | | - \_/ |
45 | | -</pre> |
46 | | - |
47 | | -<p><strong>Note</strong>: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. You don't need to understand the serialization to solve the problem.</p> |
48 | | -</div> |
| 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> |
49 | 43 |
|
50 | 44 | ### Related Topics
|
51 | 45 | [[Depth-first Search](https://github.com/openset/leetcode/tree/master/tag/depth-first-search/README.md)]
|
|
0 commit comments