MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/graph clone/solution.cpp:1–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 unordered_map<int, Node*> m; //a map to store the cloned information about a node.
3public:
4
5 Node* cloneGraph(Node* node) {
6 if(node==NULL)
7 return node;
8
9 if(m.find(node->val) != m.end()) //if the node has already been cloned, return the same.
10 return m[node->val];
11
12 Node* temp=new Node(node->val); //create a new node for the node.
13 m[node->val]=temp; //store it in the map.
14 for (auto neighbor : node->neighbors)
15 temp->neighbors.push_back(cloneGraph(neighbor)); //clone the neighbors
16
17 return temp; //return the cloned new node.
18 }
19};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected