#BST0000015. Chiều cao và BST suy biến (BST Height and Degeneration)
Chiều cao và BST suy biến (BST Height and Degeneration)
BST Height and Degeneration
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from in insertion order. Duplicate keys are ignored.
The root has depth . The depth of any other node is the number of edges on the unique path from the root to that node. The height of a non-empty tree is the maximum node depth, equivalently the number of edges on a longest path from the root to a leaf.
By convention in this task, an empty tree has height .
Compute the height after all insertions.
Input
The first line contains .
If , the next line contains signed integers . If , this line is absent.
Output
Print one integer: the height of the tree.
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): .
In all subtasks, . Duplicate keys are ignored.
Examples
Input
5
1 2 3 4 5
Output
4
Explanation
The keys are inserted in increasing order, so every new node becomes a right descendant of the previous one. The resulting tree is a chain of nodes.
The path from root to leaf contains edges, so the tree height is .