All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0133_clone_graph.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
package g0101_0200.s0133_clone_graph

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph
// #2022_10_09_Time_351_ms_(60.91%)_Space_37.1_MB_(70.56%)

import com_github_leetcode.neighbors.Node

/*
 * Definition for a Node.
 * class Node(var `val`: Int) {
 *     var neighbors: ArrayList = ArrayList()
 * }
 */
class Solution {
    fun cloneGraph(node: Node?): Node? {
        return cloneGraph(node, HashMap())
    }

    private fun cloneGraph(node: Node?, processedNodes: MutableMap): Node? {
        if (node == null) {
            return null
        } else if (processedNodes[node] != null) {
            return processedNodes[node]
        }
        val newNode = Node(node.`val`)
        processedNodes[node] = newNode
        newNode.neighbors = ArrayList()
        for (neighbor in node.neighbors) {
            val clonedNeighbor: Node? = cloneGraph(neighbor, processedNodes)
            if (clonedNeighbor != null) {
                newNode.neighbors.add(clonedNeighbor)
            }
        }
        return newNode
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy