![JAR search and dependency download from the Maven repository](/logo.png)
g0601_0700.s0652_find_duplicate_subtrees.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0601_0700.s0652_find_duplicate_subtrees
// #Medium #Hash_Table #Depth_First_Search #Tree #Binary_Tree
// #2023_02_12_Time_266_ms_(76.00%)_Space_59.8_MB_(72.00%)
import com_github_leetcode.TreeNode
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun findDuplicateSubtrees(root: TreeNode?): List {
val map: MutableMap = HashMap()
val list: MutableList = ArrayList()
helper(root, map, list)
return list
}
private fun helper(root: TreeNode?, map: MutableMap, list: MutableList): String {
if (root == null) {
return "#"
}
val key = helper(root.left, map, list) + "#" + helper(root.right, map, list) + "#" + root.`val`
map[key] = map.getOrDefault(key, 0) + 1
if (map[key] == 2) {
list.add(root)
}
return key
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy