Binary Search Tree
InterviewTime limit2sMemory limit512 MB
Insert a sequence of integers into a BST and output the depth of each inserted node.
- Level
Easy3 of 10
- Topics
- Tree, Recursion, Simulation
- Solved
- No attempts yet
Problem
Bat is a student with a strong interest in computer science, and he likes to dig deep into many subjects. This semester he is taking a data structures course, and he has come to understand how remarkable binary search trees are.
A binary search tree is a tree in which every vertex has at most two children, and it maintains the structure that the value stored in the right child is greater than or equal to the value stored in the parent, while the value stored in the left child is strictly less than the value stored in the parent.

Illustration 1: A binary search tree
Given a sequence , Bat wants to know at which depth of the binary tree these numbers are stored. Depth means the number of vertices passed through when moving from that vertex up to the topmost vertex of the tree (the root).
Can you help Bat?
Input
The first line contains (). The next line contains integers ().
Output
Output numbers. The -th number is the depth at which is stored.