Gamified challenge · Divide and conquer
blocks holds the numbers of your blocks in increasing order, all different. Build a tower that is a binary search tree: every block's left side holds only smaller numbers and its right side only bigger ones. Use every block exactly once.
The tower must be balanced: at every block, the left and right sides' heights differ by at most one floor. Write build(blocks) that returns the top block as a TreeNode (null / None for no blocks). Any balanced tower with the right blocks passes.
For [1, 2, 3, 4, 5], [3, 1, 4, null, 2, null, 5] (3 on top, 1 and 4 below it, then 2 and 5) is one right answer, and so is [3, 2, 5, 1, null, 4].
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