clone-graph.sh — zsh
graphdepth-first-searchbreadth-first-searchhashmap

Given a 1-indexed undirected graph as adjacency rows, return an independent adjacency-list copy with the same neighbor order.

Constraints

  • 0 <= number of nodes <= 100
  • No duplicate neighbors or self-loops

Follow-up

In a node-object implementation, why must the clone map be filled before recursing?

Examples
Example 1
Input: adjacency = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Example 2
Input: adjacency = [[]]
Output: [[]]
Example 3
Input: adjacency = []
Output: []
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.