DFS 76ms C++ clear solution


  • 0
    K
        unordered_map < UndirectedGraphNode *, UndirectedGraphNode * > mapa;
        
    public:
        UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
            if(!node) return node;
            
            if(mapa.find(node) != mapa.end())
                return mapa[node];
                
            UndirectedGraphNode * clone = new UndirectedGraphNode(node->label);
            mapa[node] = clone;
            
            for(auto neighbor: node->neighbors)
                clone->neighbors.push_back(cloneGraph(neighbor));
                
            return clone;
        }

Log in to reply
 

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