#K43187. Maximum Depth of a Binary Search Tree
Maximum Depth of a Binary Search Tree
Maximum Depth of a Binary Search Tree
Given a sequence of n integers, you are required to construct a Binary Search Tree (BST) by inserting the keys in the given order. After constructing the BST, compute its maximum depth. The maximum depth is defined as the length of the longest path from the root node down to the farthest leaf node.
You may use the following recursive definitions:
- For an empty tree, the depth is 0.
- For a non-empty tree, \( d = \max(d_{left}, d_{right}) + 1 \), where \( d_{left} \) and \( d_{right} \) are the depths of the left and right subtrees respectively.
Your task is to implement the construction of the BST and calculate its maximum depth.
inputFormat
The input is read from stdin and consists of:
- An integer
n
representing the number of elements in the sequence. - A line with
n
space-separated integers which are the keys to be inserted into the BST.
outputFormat
Output a single integer to stdout, representing the maximum depth of the BST.
## sample7
4 2 6 1 3 5 7
3
</p>