forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0133-clone-graph.cs
41 lines (34 loc) · 874 Bytes
/
0133-clone-graph.cs
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
/*
// Definition for a Node.
public class Node {
public int val;
public IList<Node> neighbors;
public Node() {
val = 0;
neighbors = new List<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new List<Node>();
}
public Node(int _val, List<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
public class Solution {
Dictionary<Node, Node> map = new Dictionary<Node, Node>();
public Node CloneGraph(Node node) {
if (node == null) return null;
if (!map.ContainsKey(node))
{
map.Add(node, new Node(node.val));
foreach (var n in node.neighbors)
{
map[node].neighbors.Add(CloneGraph(n));
}
}
return map[node];
}
}