-
Notifications
You must be signed in to change notification settings - Fork 0
/
133.hpp
51 lines (40 loc) · 1.07 KB
/
133.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef LEETCODE_133_HPP
#define LEETCODE_133_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
//Definition for undirected graph.
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
return clone(node);
}
private:
unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> gmap;
UndirectedGraphNode *clone(UndirectedGraphNode *node) {
if (node == nullptr)
return nullptr;
if (gmap.count(node) > 0)
return gmap[node];
UndirectedGraphNode *c = new UndirectedGraphNode(node->label);
gmap[node] = c;
for (UndirectedGraphNode *n : node->neighbors) {
c->neighbors.push_back(clone(n));
}
return c;
}
};
#endif //LEETCODE_133_HPP