🤓

2021/12/18に公開

# Question

``````Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

Assume a BST is defined as follows:

- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
``````

モードとは、最も頻繁に出現する要素のことらしい

# Code

``````/**
* 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 findMode(root: TreeNode?): IntArray {
var visited = mutableMapOf<Int, Int>()

fun dfs(node: TreeNode?) {
node?.`val`?.let {
if (visited.containsKey(it)) {
val v = visited[it] ?: return
visited[it] = v +1
} else {
visited[it] = 1
}
} ?: run { return }

node?.left?.let { dfs(it) }
node?.right?.let { dfs(it) }
}

dfs(root)

val ml = mutableListOf<Int>()
val mfn = visited.values?.max()
for ((k, v) in visited) {
}

return ml.toIntArray()

}
}
``````

DFSでとりあえず全部辿りながら、visitedというMapにノードに到達したときの値(`val`)をキーとして、valueの値にカウントする
`visited.values?.max()`で最大値を調べることはできるものの、同じカウント数である可能性があるため、これと一致するvisitedのキーを新たに抽出する必要がある

# Profile

• Runtime: 510 ms
• Memory Usage: 43.9 MB

GitHubで編集を提案