133. Clone Graph
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Graphs
- Time
- O(V + E)
- Space
- O(V)
Standard Solution (C++)
SummaryGiven a reference node in an undirected connected graph where each node holds a value and a list of its neighbors, produce a deep copy of the entire graph and return the clone of the given node.
Approach
- Use a hash map from original node pointers to their corresponding clones to track already-visited nodes and break cycles.
- Perform a DFS from the starting node: before recursing into neighbors, immediately create and register the clone so any back-edge finds it already mapped.
- For each neighbor of the original node, recursively clone it (or retrieve the existing clone) and append it to the copy's neighbor list.
- The algorithm visits every node and every edge exactly once, so it naturally handles arbitrary graph topologies including cycles.
#include <unordered_map>
#include <vector>
using namespace std;
// Node definition provided by the judge
class Node {
public:
int val;
vector<Node*> neighbors;
Node() : val(0), neighbors({}) {}
Node(int _val) : val(_val), neighbors({}) {}
Node(int _val, vector<Node*> _neighbors) : val(_val), neighbors(_neighbors) {}
};
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return nullptr;
// If already cloned, return the cached copy to handle cycles
auto it = visited.find(node);
if (it != visited.end()) return it->second;
// Create the clone and register it before visiting neighbors
Node* clone = new Node(node->val);
visited[node] = clone;
// Recursively clone each neighbor
for (Node* neighbor : node->neighbors)
clone->neighbors.push_back(cloneGraph(neighbor));
return clone;
}
private:
unordered_map<Node*, Node*> visited; // original -> clone
}; Time O(V + E) Space O(V)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
unordered_map<Node*, Node*> umap; // original node -> cloned node
Node* cloneGraph(Node* node) {
if(!node){
return nullptr;
}
auto it=umap.find(node);
if(it!=umap.end()){
return it->second;
}
Node* root=new Node(node->val);
umap[node]=root;
for(auto& n: node->neighbors){
root->neighbors.push_back(cloneGraph(n));
}
return root;
}
};
// Time Complexity: O(V + E), V is the number of nodes, E is the number of edges
// Space Complexity: O(V) for the hashmap and recursion stack Time O(V + E) Space O(V)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground C++
Your solution. A driver couldn't be auto-generated for this signature — add a main() that calls the method and prints the result, then Run.
C++ ▶ Runnable Couldn't auto-generate a driver for this signature — edit to add one.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.