Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.
Each node in the graph contains a value (int) and a list of its neighbors.
class Node {
public int val;
public List<Node> neighbors;
}
Test case format:
Example Graph:
1 --- 2
| |
4 --- 3
Adjacency list: [[2,4],[1,3],[2,4],[1,3]]
DFS Approach:
Key Insight: The hashmap serves two purposes:
adjList = [[2,4],[1,3],[2,4],[1,3]][[2,4],[1,3],[2,4],[1,3]]adjList = [[]][[]]adjList = [][]The number of nodes in the graph is in the range [0, 100].1 <= Node.val <= 100Node.val is unique for each node.There are no repeated edges and no self-loops in the graph.The Graph is connected and all nodes can be visited starting from the given node.Click "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers