133. Clone Graph
Last updated
Last updated
See:
Given a graph, return a deep copy of it.
This problem is a graph traversal problem. I used traversal to solve it. It uses the standard algorithm, but with the following changes:
an extra variable, current_clone
, is used to store the copied node. When the current_node
neighbors are traversed the standard check is done to see if the neighbor has been visited or not. If it has not been added then a new Node is created with the value. This is the copied value. In both cases the neighbor is added to the current_clone
neighbors list.
The visited hash value stores a copy of a Node. In the standard algorithm it is simply stores True. This hash stores the deep copy values.