1: /**
2: * Definition for undirected graph.
3: * struct UndirectedGraphNode {
4: * int label;
5: * vector<UndirectedGraphNode *> neighbors;
6: * UndirectedGraphNode(int x) : label(x) {};
7: * };
8: */
9: class Solution {
10: public:
11: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
12: if (node == NULL) return NULL;
13: unordered_map<int, UndirectedGraphNode*> visited;
14: return dfs(node, visited);
15: }
16: UndirectedGraphNode *dfs(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &visited) {
17: UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
18: visited[node->label] = newNode;
19: for (UndirectedGraphNode* n : node->neighbors) {
20: if (!visited[n->label]) {
21: newNode->neighbors.push_back(dfs(n, visited));
22: } else {
23: newNode->neighbors.push_back(visited[n->label]);
24: }
25: }
26: return newNode;
27: }
28: };
Here is the BFS way.
1: /**
2: * Definition for undirected graph.
3: * struct UndirectedGraphNode {
4: * int label;
5: * vector<UndirectedGraphNode *> neighbors;
6: * UndirectedGraphNode(int x) : label(x) {};
7: * };
8: */
9: class Solution {
10: public:
11: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
12: if (node == NULL) return NULL;
13: unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> mp;
14: UndirectedGraphNode *root = new UndirectedGraphNode(node->label);
15: mp[node] = root;
16: queue<UndirectedGraphNode *> q;
17: q.push(node);
18: while(!q.empty()) {
19: UndirectedGraphNode *cur = q.front();
20: q.pop();
21: for (UndirectedGraphNode *neighbor : cur->neighbors) {
22: if (mp.find(neighbor) == mp.end()) {
23: UndirectedGraphNode *copy = new UndirectedGraphNode(neighbor->label);
24: mp[neighbor] = copy;
25: q.push(neighbor);
26: }
27: mp[cur]->neighbors.push_back(mp[neighbor]);
28: }
29: }
30: return root;
31: }
32: };
When I revisited this problem again, I found a more concise DFS way.
1: /**
2: * Definition for undirected graph.
3: * struct UndirectedGraphNode {
4: * int label;
5: * vector<UndirectedGraphNode *> neighbors;
6: * UndirectedGraphNode(int x) : label(x) {};
7: * };
8: */
9: class Solution {
10: private:
11: unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> mp;
12: public:
13: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
14: if (node == NULL) return NULL;
15: UndirectedGraphNode *root = new UndirectedGraphNode(node->label);
16: mp[node] = root;
17: for (UndirectedGraphNode *n : node->neighbors) {
18: if (mp.find(n) == mp.end()) root->neighbors.push_back(cloneGraph(n));
19: else root->neighbors.push_back(mp[n]);
20: }
21: return root;
22: }
23: };
No comments:
Post a Comment