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

g0601_0700.s0652_find_duplicate_subtrees.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
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