| 1 | class Solution { |
| 2 | unordered_map<int, Node*> m; //a map to store the cloned information about a node. |
| 3 | public: |
| 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 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected