#include using namespace std; class Node { public: int val; vector neighbors; Node() { val = 0; neighbors = vector(); } Node(int _val) { val = _val; neighbors = vector(); } Node(int _val, vector _neighbors) { val = _val; neighbors = _neighbors; } }; class Solution { public: Node *cloneGraph(Node *node) { unordered_map m; return helper(node, m); } Node *helper(Node *node, unordered_map &m) { if (!node) return NULL; if (m.count(node)) return m[node]; Node *clone = new Node(node->val); m[node] = clone; for (Node *neighbor : node->neighbors) { clone->neighbors.push_back(helper(neighbor, m)); } return clone; } };