You're given a graph node, and you need to produce a complete copy of the graph: new node objects with the same values and the same connections. The complication is that graphs can have cycles. If node 1 points to node 2, and node 2 points back to node 1, a copy routine that recurses into neighbors without any bookkeeping loops between the two forever.
Most of the work is tracking which nodes already have clones, so that the copy contains no duplicates and the traversal terminates. Each time you reach a node, one question decides what to do: does this node already have a clone? If yes, return the existing clone. If no, create one, then clone its neighbors.
A hash map from original node to cloned node covers both needs. It acts as a visited set that stops the traversal from looping, and as a lookup table for wiring each clone's neighbor references to the correct clone objects.
0 <= number of nodes <= 100: The graph is small enough that even a quadratic solution would pass, but standard graph traversal gives O(V + E) anyway. The zero case means the input can be null, which both solutions check first.1 <= Node.val <= 100 and values are unique: Unique values mean the value could serve as a hash map key, but keying on the node reference itself works even when values repeat, so the solutions below use the reference.Traverse the graph with BFS, cloning nodes as they are discovered. Start by cloning the given node and seeding the queue with the original.
When we dequeue a node, we iterate through its neighbors. If a neighbor has no clone yet, we create one, record it in the map, and enqueue the neighbor so its own edges get processed later. Either way, we append the cloned neighbor to the dequeued node's clone. When the queue is empty, every node has been cloned and every edge has been copied in both directions.
null, return null.The trace below runs BFS on the four-node graph from Example 1, where nodes 1 and 3 each connect to nodes 2 and 4. The second panel shows the hash map filling up as clones are created.
DFS reaches the same O(V + E) bound with less bookkeeping: the call stack replaces the explicit queue.
Write a function whose contract is "return the clone of this node". If the node is already in the map, return the existing clone. Otherwise, create the clone, add it to the map, then recursively clone each neighbor and append the results to the new clone's neighbor list.
The one ordering requirement is that the clone goes into the map before the recursive calls.
Registering the clone before recursing is what prevents infinite loops. When a cycle leads back to a node whose clone exists but whose neighbor list is still being filled, the map lookup returns that existing clone instead of recursing again. This is the same principle as marking a node visited before exploring its edges in standard DFS. Each node is cloned exactly once, and each directed neighbor entry produces exactly one append, so the copy has the same edges as the original.
null, return null.The same graph from Example 1, cloned with DFS this time. The recursion goes deep along the path 1, 2, 3, 4 before unwinding, so the visit order differs from BFS even though the resulting copy is identical.