Skip to content

Commit 34171c8

Browse files
committed
Time: 235 ms (70.37%), Space: 40.7 MB (40.74%) - LeetHub
1 parent 495d738 commit 34171c8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
private val counter = mutableMapOf<Int, Int>()
3+
4+
private fun TreeNode?.count() {
5+
if (this == null)
6+
return
7+
counter[this.`val`] = (counter[this.`val`] ?: 0) + 1
8+
left.count()
9+
right.count()
10+
}
11+
12+
fun findMode(root: TreeNode?): IntArray {
13+
root.count()
14+
val max = counter.values.max()
15+
return counter
16+
.filter { (_, v) -> v == max }
17+
.keys
18+
.toIntArray()
19+
}
20+
}

0 commit comments

Comments
 (0)