Skip to content

Commit 90fca47

Browse files
authored
Create 133. Clone Graph 8Apr
133. Clone Graph
1 parent 824804d commit 90fca47

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

133. Clone Graph 8Apr

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
Node* cloneGraph(Node* node) {
4+
if(!node) return node;
5+
unordered_map<Node*,Node*> m;
6+
Node* ans = new Node(node->val,{});
7+
m[node]=ans;
8+
queue<Node*> q;
9+
q.push(node);
10+
while(!q.empty()){
11+
Node* currNode=q.front();
12+
q.pop();
13+
vector<Node*> neigh = currNode->neighbors;
14+
for(auto x:neigh){
15+
if(m.find(x)==m.end()){
16+
m[x] = new Node(x->val,{});
17+
q.push(x);
18+
}
19+
m[currNode]->neighbors.push_back(m[x]);
20+
}
21+
}
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)