leetcode-133. Clone Graph


Clone an undirected graph. Each node in the graph contains a  label
 and a list of its  neighbors
.
Clone an undirected graph. Each node in the graph contains a  label  and a list of its  neighbors .
#include
#include
#include
#include

using namespace std;

struct UndirectedGraphNode {
     int label;
     vector neighbors;
     UndirectedGraphNode(int x) : label(x) {};
 };

UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node) {
	if (node == NULL) {
	     return NULL;
	}
 
	map gphMap;
	queue gphQue;
         
    gphQue.push(node);
    gphMap[node] = new UndirectedGraphNode(node->label);
    while (!gphQue.empty()) {
        const UndirectedGraphNode *tmp = gphQue.front();
        gphQue.pop();
 
        for (int i = 0; i != tmp->neighbors.size(); ++i){
            if (gphMap.find(tmp->neighbors[i]) == gphMap.end()){
            //build the vertex
                UndirectedGraphNode *newNode = new UndirectedGraphNode(tmp->neighbors[i]->label);
                gphMap[tmp->neighbors[i]] = newNode;
                gphMap[tmp]->neighbors.push_back(newNode);            //Adjust the Vertex    
            	gphQue.push(tmp->neighbors[i]);
            }
            else{
            	gphMap[tmp]->neighbors.push_back(gphMap[tmp->neighbors[i]]);
            }
        }
    }
 
         return gphMap[node];
     }

int main(){
	
	
	return 0;
}