Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,19 @@ private data class TreeNodeRelative(
val sumLevels: List<Int>,
val parentToChildrenSum: Map<TreeNode, Int>
)

/**
* 617. Merge Two Binary Trees
*/

fun mergeTrees(root1: TreeNode?, root2: TreeNode?): TreeNode? {
if (root1 == null && root2 == null) return null
root1 ?: return root2
root2 ?: return root1

val newRoot = TreeNode(root1.`val` + root2.`val`)
newRoot.left = mergeTrees(root1?.left, root2?.left)
newRoot?.right = mergeTrees(root1?.right, root2?.right)

return newRoot
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ fun isCousinsProdVariant(root: TreeNode?, x: Int, y: Int): Boolean {
}

return false
}

/**
* 617. Merge Two Binary Trees
*/

fun mergeTreesProdVariant(root1: TreeNode?, root2: TreeNode?): TreeNode? = when {
root1 == null && root2 == null -> null
root1 == null -> root2
root2 == null -> root1
else -> {
val newRoot = TreeNode(root1.`val` + root2.`val`)
newRoot.left = mergeTreesProdVariant(root1?.left, root2.left)
newRoot.right = mergeTreesProdVariant(root1?.right, root2?.right)
newRoot
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,26 @@ fun isVowel(char: Char) = when {
char in "aeiou" -> true
else -> false
}

/**
* 3005. Count Elements With Maximum Frequency
*/

fun maxFrequencyElements(nums: IntArray): Int {
if (nums.hasSingle()) return 1

val freq = nums.toList().groupingBy { it }.eachCount()
var max = 0
var res = 0

for ((_, value) in freq) max = maxOf(max, value)

for ((_, value) in freq) {
if (value == max) res += value
}

return res

}