Clone Graph

Using BFS

The main idea is to traverse each node using BFS and build its list of neighbours. The catch is to avoid rebuilding any node when it reappears as some other node's child while it has already been processed. If not avoided, this will result in an infinite loop.

Without Comments

Time Complexity: O(N+E)O(N + E)​

Space Complexity: O(N+E)+O(N)+O(N)O(N + E) + O(N) + O(N)​ for BFS, queue and oldToNew mapping

Last updated