Class Solution

java.lang.Object
g0101_0200.s0133_clone_graph.Solution

public class Solution extends Object
133 - Clone Graph.<p>Medium</p> <p>Given a reference of a node in a <strong><a href="https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph" target="_top">connected</a></strong> undirected graph.</p> <p>Return a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_top"><strong>deep copy</strong></a> (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> <p>class Node { public int val; public List<Node> neighbors; }</p> <p><strong>Test case format:</strong></p> <p>For simplicity, each node&rsquo;s value is the same as the node&rsquo;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><strong>An adjacency list</strong> is a collection of unordered <strong>lists</strong> 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 <code>val = 1</code>. You must return the <strong>copy of the given node</strong> as a reference to the cloned graph.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" alt="" /></p> <p><strong>Input:</strong> adjList = [[2,4],[1,3],[2,4],[1,3]]</p> <p><strong>Output:</strong> [[2,4],[1,3],[2,4],[1,3]]</p> <p><strong>Explanation:</strong></p> <pre><code> 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). </code></pre> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/01/07/graph.png" alt="" /></p> <p><strong>Input:</strong> adjList = [[]]</p> <p><strong>Output:</strong> [ []]</p> <p><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.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> adjList = []</p> <p><strong>Output:</strong> []</p> <p><strong>Explanation:</strong> This an empty graph, it does not have any nodes.</p> <p><strong>Example 4:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/01/07/graph-1.png" alt="" /></p> <p><strong>Input:</strong> adjList = [[2],[1]]</p> <p><strong>Output:</strong> [[2],[1]]</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 <= Node.val <= 100</code></li> <li><code>Node.val</code> 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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • cloneGraph

      public Node cloneGraph(Node node)