 All Problems
Clone Graph
medium
hash table
depth-first search
breadth-first search
graph
facebook
amazon
google

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 (List[Node]) of its neighbors.

Example 1:

Input:
4
1: 2 4
2: 1 3
3: 2 4
4: 1 3
Output:
1: 2 4
2: 1 3
3: 2 4
4: 1 3

Constraints:

  • The number of nodes in the graph is in the range [0, 100]
  • 1 ≤ Node.val ≤ 100
  • Node.val is unique for each node
  • No repeated edges and no self-loops
  • The graph is connected

Input format: First line: number of nodes n. Then n lines each in format "node: neighbor1 neighbor2 ..." (no neighbors means just "node:").

Output format: Same adjacency list format sorted by node value, neighbors sorted ascending.

Run to check your code against the sample cases, or submit to run every case