Simple c++ solution


  • 0
        map<UndirectedGraphNode *, UndirectedGraphNode *> m;
        UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
            if (!node) return NULL;
            if (m[node]) return m[node];
            UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
            m[node] = res;
            for (UndirectedGraphNode *x : node->neighbors) {
                res->neighbors.push_back(cloneGraph(x));
            }
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.