Gamified challenge · BST descent
tree is a binary search tree of different positive prices in level order with null for a missing spot (TreeNode in code): smaller prices to the left, bigger to the right. budget is a positive integer.
Write floorCeil(tree, budget) (Python: floor_ceil(tree, budget)) that returns [floor, ceiling]: the floor is the highest price <= budget, the ceiling the lowest price >= budget. Use -1 for one that doesn't exist.
For [8, 4, 12, 2, 6, 10, 14]: a budget of 5 gives [4, 6], 10 gives [10, 10], and 1 gives [-1, 2].
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google