Problem 604
Find Mode in Binary Search Tree
Easy · 10 pts · Tree, Binary Search Tree, Depth-First Search
Problem
The binary search tree may contain duplicate values: every value in a node's left subtree is smaller than the node, and every value in its right subtree is greater than or equal to it. Find the mode, the value that appears most often. If several values tie, print all of them in increasing order. An in-order traversal visits the values in sorted order, so equal values come together and can be counted with a running streak, without any extra map.
Input
The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.
Output
Print the mode values in increasing order, separated by spaces.
Constraints
1 <= number of nodes <= 10000; -100000 <= value <= 100000
Example input
4 1 null 2 2
Example output
2
Explanation
The tree 1 null 2 2 holds the values 1, 2 and 2. The value 2 appears twice and 1 only once, so the mode is 2.