Easy and Clean C++ Code


  • 1
    H
     class Solution {
        private:
        	map<int,UndirectedGraphNode *> f;
        public:
        	UndirectedGraphNode* clone(UndirectedGraphNode *node){
        		if(node==NULL) return NULL;
        		if(f.count(node->label)) return f[node->label];
        		UndirectedGraphNode *temp=new UndirectedGraphNode(node->label);
        		f.insert(make_pair(node->label,temp));
        		for(int i=0;i<int((node->neighbors).size());i++)
        			temp->neighbors.push_back(clone(node->neighbors[i]));
        		return temp;
        	}
            UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        		return clone(node);
            }
        };

Log in to reply
 

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