Algorithms and Data Structures

• Status
• 1
Implementation
• 2
Bitwise operations
• 3
Prefix Sums
• 4
Sliding window / Two pointers
• 5
Modular Arithmetic
• 6
Number Theory
• 7
Binary Search
• 8
Basic Sorting
• 9
Greedy Algorithms
• 10
Basic Dynamic Programming
• 11
Recursion
• 12
• 13
Queue & Stack
• 14
Binary tree + BST
• 15
Divide & Conquer + Advanced Sorting
• 16
Heap
• 17
Hashing
• 18
Graph Representation
• 19
BFS

• 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: 2.5 seconds

Memory limit: 512 MB

Output limit: 1 MB