Loading problem...
You are given the root node of a Binary Search Tree (BST) that may contain duplicate values. Your task is to identify and return all most frequently occurring values (often referred to as modes) within the tree.
If multiple values share the highest frequency (i.e., they appear equally often and more than any other values), return all of them in your result array. The order of the returned values does not matter.
For the purpose of this problem, a Binary Search Tree is defined with the following properties:
Note: This definition allows for duplicate values, where equal values may appear in either subtree.
root = [1,null,2,2][2]The tree has nodes with values 1, 2, and 2. The value 2 appears twice (highest frequency), while 1 appears once. Therefore, the only mode is 2.
root = [0][0]The tree contains a single node with value 0. Since it's the only value, it is trivially the most frequent.
root = [1,1,2,null,null,2][1,2]The tree has values 1 appearing twice (root and left child) and 2 appearing twice (right child and its left child). Both 1 and 2 tie for the highest frequency, so both are modes.
Constraints