Dare2Solve
The problem involves cloning an undirected graph. Each node in the graph contains a value and a list of its neighbors. Given a reference to a node in the graph, we need to create a deep copy of the graph and return the reference to the cloned node.
The graph can be represented using a depth-first search (DFS) or breadth-first search (BFS) traversal. During the traversal, each node can be cloned, and its neighbors can be recursively cloned. Using a dictionary to map original nodes to their clones ensures that each node is cloned only once and helps to avoid infinite recursion for cyclic graphs.
O(N + E), where N is the number of nodes and E is the number of edges in the graph. Each node and each edge is visited once during the DFS traversal.
O(N), where N is the number of nodes in the graph. This is due to the extra space used by the dictionary to store the mapping from original nodes to their clones, and the recursion stack.
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return nullptr;
unordered_map<Node*, Node*> oldToNew;
return dfs(node, oldToNew);
}
private:
Node* dfs(Node* node, unordered_map<Node*, Node*>& oldToNew) {
if (oldToNew.find(node) != oldToNew.end()) {
return oldToNew[node];
}
Node* copy = new Node(node->val);
oldToNew[node] = copy;
for (Node* neighbor : node->neighbors) {
copy->neighbors.push_back(dfs(neighbor, oldToNew));
}
return copy;
}
};
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
if not node:
return None
oldToNew = {}
def dfs(node):
if node in oldToNew:
return oldToNew[node]
copy = Node(node.val)
oldToNew[node] = copy
for neighbor in node.neighbors:
copy.neighbors.append(dfs(neighbor))
return copy
return dfs(node)
class Solution {
public Node cloneGraph(Node node) {
if (node == null) {
return null;
}
Map<Node, Node> oldToNew = new HashMap<>();
return dfs(node, oldToNew);
}
private Node dfs(Node node, Map<Node, Node> oldToNew) {
if (oldToNew.containsKey(node)) {
return oldToNew.get(node);
}
Node copy = new Node(node.val);
oldToNew.put(node, copy);
for (Node neighbor : node.neighbors) {
copy.neighbors.add(dfs(neighbor, oldToNew));
}
return copy;
}
}
var cloneGraph = function (node) {
let oldToNew = new Map();
return node ? dfs(node) : null;
function dfs(node) {
if (oldToNew.has(node)) {
return oldToNew.get(node);
}
let copy = new _Node(node.val);
oldToNew.set(node, copy);
for (let nei of node.neighbors) {
copy.neighbors.push(dfs(nei));
}
return copy;
}
};