Skip to content
BASA DSA

Clone Graph

MediumGraphs

Deep-copy the connected undirected graph reachable from node. Return a new entry node preserving labels and adjacency, with no original nodes reused. Input adjacency row i lists neighbors of label i+1; empty input represents null.

Example 1

Undirected adjacency list: row i lists neighbors of vertex i + 1.Undirected adjacency list: row i lists neighbors of vertex i + 1. Vertices: node 0: 1, node 1: 2, node 2: 3. Edges: node 0 (1) — node 1 (2); node 0 (1) — node 2 (3); node 1 (2) — node 2 (3).123
Undirected adjacency list: row i lists neighbors of vertex i + 1.

Input
node = [[2,3],[1,3],[1,2]]
Output
[[2,3],[1,3],[1,2]]

Explanation

Allocate three fresh nodes and reproduce the triangle's six adjacency entries.

Example 2

Undirected adjacency list: row i lists neighbors of vertex i + 1.Undirected adjacency list: row i lists neighbors of vertex i + 1. Vertices: node 0: 1. Edges: none.1
Undirected adjacency list: row i lists neighbors of vertex i + 1.

Input
node = [[]]
Output
[[]]

Explanation

The isolated node still needs a fresh copy despite having no neighbors.

Example 3

Undirected adjacency list: row i lists neighbors of vertex i + 1. No nodes or connections.
Input
node = []
Output
null

Explanation

A null graph copies to null rather than an isolated node.

Constraints

  • 0 <= number of nodes <= 100
  • Labels are 1..n
  • Graph is connected, simple and undirected

Function contractcloneGraphArguments: graph
Loading draft…
SolutionRuns on your device
Loading draft…