1021. Clone an Undirected Graph

EasyGraphsDFSBFSGraph

Given an undirected graph with n nodes and a list of edges, produce a deep copy of the graph and return the clone's adjacency list — for each node, its sorted list of neighbors. (A correct clone has the identical structure.) The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Nested array — the clone's adjacency list.

Examples

Example 1
Input: {"n":4,"edges":[[0,1],[1,2],[2,3],[3,0]]}
Output: [[1,3],[0,2],[1,3],[0,2]]
Explanation: Cloned 4-cycle adjacency.
Example 2
Input: {"n":1,"edges":[]}
Output: [[]]
Explanation: Single isolated node.
Example 3
Input: {"n":2,"edges":[[0,1]]}
Output: [[1],[0]]
Explanation: Single edge cloned.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →