<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>clone Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/clone/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/clone/</link>
	<description></description>
	<lastBuildDate>Mon, 15 Aug 2022 03:41:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>clone Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/clone/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 133. Clone Graph</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-133-clone-graph-2/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-133-clone-graph-2/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 14 Aug 2022 16:33:26 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9775</guid>

					<description><![CDATA[<p>Given a reference of a node in a connected undirected graph. Return a&#160;deep copy&#160;(clone) of the graph. Each node in the graph contains a value (int) and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-133-clone-graph-2/">花花酱 LeetCode 133. Clone Graph</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 133. Clone Graph - 刷题找工作 EP400" width="500" height="281" src="https://www.youtube.com/embed/NCQMg-WMRZc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



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



<p>Return a&nbsp;<a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank" rel="noreferrer noopener"><strong>deep copy</strong></a>&nbsp;(clone) of the graph.</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="wp-block-preformatted;crayon:false">class Node {
    public int val;
    public List<Node> neighbors;
}
</pre>



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



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



<p><strong>An adjacency list</strong>&nbsp;is a collection of unordered&nbsp;<strong>lists</strong>&nbsp;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 always be the first node with&nbsp;<code>val = 1</code>. You must return the&nbsp;<strong>copy of the given node</strong>&nbsp;as a reference to the cloned graph.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><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.
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/07/graph.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> adjList = [[]]
<strong>Output:</strong> [[]]
<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.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> adjList = []
<strong>Output:</strong> []
<strong>Explanation:</strong> This an empty graph, it does not have any nodes.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The number of nodes in the graph is in the range&nbsp;<code>[0, 100]</code>.</li><li><code>1 &lt;= Node.val &lt;= 100</code></li><li><code>Node.val</code>&nbsp;is unique for each node.</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>



<h2><strong>Solution: DFS + Hashtable</strong></h2>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-1.png" alt="" class="wp-image-9776" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-2.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-2.png" alt="" class="wp-image-9777" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-3.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-3.png" alt="" class="wp-image-9778" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/08/133-ep400-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>Time complexity: O(V+E)<br>Space complexity: O(V+E)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  Node* cloneGraph(Node* node) {
    if (!node) return nullptr;
    unordered_map&lt;Node*, Node*&gt; m;
    function&lt;void(Node*)&gt; dfs = [&amp;](Node* u) {
      m[u] = new Node(u-&gt;val);
      for (Node* v : u-&gt;neighbors) {
        if (!m.count(v)) dfs(v);
        m[u]-&gt;neighbors.push_back(m[v]);
      }
    };
    dfs(node);
    return m[node];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-133-clone-graph-2/">花花酱 LeetCode 133. Clone Graph</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-133-clone-graph-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
