Problem Description
Given the root of a binary search tree (BST) where duplicates are allowed, return all the mode(s) (i.e., the most frequently occurred element) found in the tree. In a BST, the left subtree of a node contains only nodes with keys less than or equal to the node's key, and the right subtree only contains keys greater than or equal to the node's key.
Key Insights
- The BST in-order traversal yields a sorted order, which helps in counting consecutive duplicates.
- A two-pass in-order traversal can be used: one pass to determine the maximum frequency (maxCount) and a second pass to collect all values that match this frequency.
- The solution can be implemented without extra space (apart from recursion stack) by leveraging constant space counters and state variables.
Space and Time Complexity
Time Complexity: O(n), where n is the number of nodes, because each node is processed twice (once in each traversal). Space Complexity: O(h) where h is the height of the tree due to the recursion stack. O(1) extra space is used aside from this.
Solution
We perform an in-order traversal of the BST twice. The first traversal determines the maximum frequency of any value in the BST by comparing the current node's value with the previously visited node. Since the in-order traversal processes nodes in sorted order, duplicate values are consecutive, making the counting simple. After calculating maxCount, we reset our state and perform a second in-order traversal to collect all node values with frequency equal to maxCount. The key trick is to leverage the BST property (sorted in-order traversal) to count frequencies in one pass without using additional data structures like hash maps.