标签:return cte clone for rap nod pre label dfs
/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector<UndirectedGraphNode *> neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */ class Solution { public: unordered_map<UndirectedGraphNode *,UndirectedGraphNode *> m; unordered_set<UndirectedGraphNode *> visited; UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) return NULL; dfs_create(node); dfs_link(node); return m[node]; } void dfs_create(UndirectedGraphNode *node) { if (node == NULL || m.count(node)) return; UndirectedGraphNode *p = new UndirectedGraphNode(node->label); m[node] = p; for (auto n : node->neighbors) dfs_create(n); } void dfs_link(UndirectedGraphNode *node) { if (node == NULL || visited.count(node)) return; visited.insert(node); for (auto n : node->neighbors) { m[node]->neighbors.push_back(m[n]); dfs_link(n); } } };
标签:return cte clone for rap nod pre label dfs
原文地址:https://www.cnblogs.com/JTechRoad/p/9063544.html