-
Notifications
You must be signed in to change notification settings - Fork 88
/
cloneGraph.cpp
46 lines (41 loc) · 999 Bytes
/
cloneGraph.cpp
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
/* mbinary
#########################################################################
# File : cloneGraph.cpp
# Author: mbinary
# Mail: zhuheqin1@gmail.com
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2019-04-16 09:41
# Description:
#########################################################################
*/
class Solution
{
public:
map<Node*, Node*> st;
Node *cloneGraph(Node *node)
{
Node* ret = new Node(node->val, vector<Node*>());
st[node] = ret;
for (auto x : node->neighbors) {
auto p = st.find(x);
if (p == st.end()) {
ret->neighbors.push_back(cloneGraph(x));
} else ret->neighbors.push_back(p->second);
}
return ret;
}
};
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/