A boutique wine cellar keeps its bottles arranged in a binary search tree keyed by vintage year: for every bottle, every year stored to its left is never larger than its own year, and every year stored to its right is never smaller. Because different bottles can share the same vintage, some years may appear more than once in the arrangement. The cellar master wants to know which vintage year (or years, in case of a tie) is the most represented in the whole rack.
Line 1: a single integer T, the number of tokens describing the tree. Line 2: T space-separated tokens giving the tree in preorder form with explicit null markers. The description of a subtree is either the single token N (an empty subtree) or a bottle's vintage year followed by the description of its left subtree and then the description of its right subtree. The root subtree is never empty (there is always at least one bottle).
Print the vintage year(s) that occur most often among all bottles in the rack, in strictly increasing numeric order, separated by single spaces, on one line. (If every year occurs exactly once, every year is a mode, so print all of them in increasing order.)
1 <= number of bottles (real, non-N nodes) <= 5000 1900 <= vintage year <= 2025 The tokens always describe a valid binary search tree (repeated years allowed) using the preorder-with-null-markers format described above.
Example 1
Input
9 2010 2005 N N 2015 2015 N N N
Expected
2015
Explanation
The rack holds four bottles with vintage years 2010, 2005, 2015, and 2015 (the second 2015 is the left child of the first 2015 node). The year 2015 appears twice, while 2010 and 2005 each appear once, so 2015 is the sole mode. Output: "2015".
Example 2
Input
7 1950 1920 N N 1980 N N
Expected
1920 1950 1980
Explanation
The rack holds three bottles with distinct vintage years 1950, 1920, and 1980, each appearing exactly once. Since the maximum frequency (1) is shared by all three years, every year is a mode; printed in increasing order: "1920 1950 1980".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →