133. Clone Graph
Description
See: https://leetcode.com/problems/clone-graph/
Given a graph, return a deep copy of it.
Solution
This problem is a graph traversal problem. I used BFS 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 thecurrent_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 thecurrent_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.
Last updated