Zero To DSAZero To DSA
Privacy Policy路FAQ路Report a Bug路Support on Ko鈥慺i
Number of IslandsCourse Schedule

Clone Graph

medium
Time: O(V + E)
Space: O(V)

Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.

C#C#
public class Node {
    public int val;
    public IList<Node> neighbors;
    public Node(int _val, IList<Node> _neighbors = null) {
        val = _val;
        neighbors = _neighbors ?? new List<Node>();
    }
}

public Node CloneGraph(Node node) {
    // TODO: implement your solution here
    return null;
}

Constraints

  • The number of nodes is in the range [0, 100].

Examples

Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
The graph has 4 nodes.