Loading...
Loading...
Rahul works part-time at a mobile shop in Chennai. The shop keeps phone prices in a binary search tree, inserted in the given order into an empty tree.
Customers come with a budget x. For each budget, print the floor: the highest price that is at most x. Also print the ceiling: the lowest price that is at least x. Print NONE when no such price exists.
NONE.Example
Input
5 50 30 70 20 40 4 45 40 10 90
Output
40 50 40 40 NONE 20 70 NONE
For 45 the floor is 40 and the ceiling 50. 40 is a price, so it is both. Nothing is at most 10, and nothing is at least 90.
Try the problem first. Open one hint at a time.
Walk down from the root as in a search. A node with a price at most x could be the floor, and a node with a price at least x could be the ceiling.
When a node is at most x, remember it and go right to look for a closer one. When it is larger, go left.
If you meet a node equal to x, it is both the floor and the ceiling.
Find the floor and the ceiling with two separate walks to keep the code simple.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor