Description

Given the root of a binary search tree with duplicates, return all the modes (the most frequently occurred element) in it. If the tree has more than one mode, return them in any order.

Examples

Input:root = [1,null,2,2]
Output:[2]
Explanation:

2 appears twice.

Input:root = [5,3,7,3,null,6,8]
Output:[3]
Explanation:

The value 3 appears twice in the BST (as left child of root and left child of left subtree), while all other values (5, 7, 6, 8) appear only once. Therefore, 3 is the most frequent element.

Input:root = [4,2,6,1,3,5,7]
Output:[1,2,3,4,5,6,7]
Explanation:

All values in this BST appear exactly once with the same frequency. When there is a tie for the highest frequency, all values with that maximum frequency are returned.

Constraints

  • 1 ≤ nodes ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!