forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} |