forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyhkee0404.scala
More file actions
33 lines (31 loc) · 810 Bytes
/
yhkee0404.scala
File metadata and controls
33 lines (31 loc) · 810 Bytes
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
import scala.collection.mutable.ListBuffer
/**
* Definition for a Node.
* class Node(var _value: Int) {
* var value: Int = _value
* var neighbors: List[Node] = List()
* }
*/
object Solution {
def cloneGraph(graph: Node): Node = {
if (graph == null) {
return null
}
val dp = Array.fill[Node](101)(null)
cloneGraph(dp, graph)
}
def cloneGraph(dp: Array[Node], graph: Node): Node = {
if (dp(graph.value) != null) {
return dp(graph.value)
}
val u = Node(graph.value)
dp(graph.value) = u
val neighbors = ListBuffer[Node]()
graph.neighbors
.foreach {
neighbors += cloneGraph(dp, _)
}
u.neighbors ++= neighbors
u
}
}