Find the Mode in a Binary Search Tree

Given a binary search tree, you are asked to find the most common element (the mode) of the values in the binary search tree.
Equal values are always stored on the right subtree of the node. So, if the value of a child node is equal to its parent’s value, it’s guaranteed that the child is the right child. Note that equal values are not always guaranteed to appear right next to each other.

Input

The input is handled automatically, you don’t need to do anything. It’s guaranteed that the input binary tree is valid.

Output

The program should print the most common element of the values in the binary search tree. If there are several such answers, the program should print the smallest one.

Examples

Input
Output
7 3 1 2 4 2 3 2
2
 

Constraints

Time limit: 5 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue