Skip to content

Commit

Permalink
Create 0133-clone-graph.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Dec 29, 2022
1 parent 324cd74 commit 45359d2
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions kotlin/0133-clone-graph.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//recursive version
class Solution {
fun cloneGraph(node: Node?): Node? {
if(node == null)
return null
val hm = HashMap<Node, Node>()
return clone_dfs(hm, node)
}
private fun clone_dfs(hm: HashMap<Node,Node>, oldNode: Node?): Node?{
if(hm.contains(oldNode))
return hm.get(oldNode)
val copy = Node(oldNode!!.`val`)
hm[oldNode] = copy
for(n in oldNode.neighbors)
copy.neighbors.add(clone_dfs(hm, n))
return copy
}
}

//dfs with queue
class Solution {
fun cloneGraph(node: Node?): Node? {
if(node == null)
return null
val hm = HashMap<Node, Node>()
val q = ArrayDeque<Node>()
q.add(node)
hm[node] = Node(node!!.`val`)
while(!q.isEmpty()){
val current = q.poll()
for(n in current.neighbors){
if(!hm.contains(n)){
val copy = Node(n!!.`val`)
hm[n] = copy
q.add(n)
}
hm[current]!!.neighbors.add(hm[n])
}
}
return hm[node]
}
}

//bfs with queue
class Solution {
fun cloneGraph(node: Node?): Node? {
if(node == null)
return null
val hm = HashMap<Node, Node>()
val q = ArrayDeque<Node>()
q.addLast(node)
hm[node] = Node(node!!.`val`)
while(!q.isEmpty()){
val current = q.removeLast()
for(n in current.neighbors){
if(!hm.contains(n)){
val copy = Node(n!!.`val`)
hm[n] = copy
q.add(n)
}
hm[current]!!.neighbors.add(hm[n])
}
}
return hm[node]
}
}

0 comments on commit 45359d2

Please sign in to comment.