Skip to content

Commit 25ed040

Browse files
Merge pull request #218
add new problem 22.09
2 parents 940cffa + 99f0518 commit 25ed040

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,19 @@ private data class TreeNodeRelative(
244244
val sumLevels: List<Int>,
245245
val parentToChildrenSum: Map<TreeNode, Int>
246246
)
247+
248+
/**
249+
* 617. Merge Two Binary Trees
250+
*/
251+
252+
fun mergeTrees(root1: TreeNode?, root2: TreeNode?): TreeNode? {
253+
if (root1 == null && root2 == null) return null
254+
root1 ?: return root2
255+
root2 ?: return root1
256+
257+
val newRoot = TreeNode(root1.`val` + root2.`val`)
258+
newRoot.left = mergeTrees(root1?.left, root2?.left)
259+
newRoot?.right = mergeTrees(root1?.right, root2?.right)
260+
261+
return newRoot
262+
}

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,20 @@ fun isCousinsProdVariant(root: TreeNode?, x: Int, y: Int): Boolean {
6161
}
6262

6363
return false
64+
}
65+
66+
/**
67+
* 617. Merge Two Binary Trees
68+
*/
69+
70+
fun mergeTreesProdVariant(root1: TreeNode?, root2: TreeNode?): TreeNode? = when {
71+
root1 == null && root2 == null -> null
72+
root1 == null -> root2
73+
root2 == null -> root1
74+
else -> {
75+
val newRoot = TreeNode(root1.`val` + root2.`val`)
76+
newRoot.left = mergeTreesProdVariant(root1?.left, root2.left)
77+
newRoot.right = mergeTreesProdVariant(root1?.right, root2?.right)
78+
newRoot
79+
}
6480
}

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,26 @@ fun isVowel(char: Char) = when {
662662
char in "aeiou" -> true
663663
else -> false
664664
}
665+
666+
/**
667+
* 3005. Count Elements With Maximum Frequency
668+
*/
669+
670+
fun maxFrequencyElements(nums: IntArray): Int {
671+
if (nums.hasSingle()) return 1
672+
673+
val freq = nums.toList().groupingBy { it }.eachCount()
674+
var max = 0
675+
var res = 0
676+
677+
for ((_, value) in freq) max = maxOf(max, value)
678+
679+
for ((_, value) in freq) {
680+
if (value == max) res += value
681+
}
682+
683+
return res
684+
685+
}
686+
687+

0 commit comments

Comments
 (0)