Build a balanced BST from an array · Compiler
Build a balanced BST from an array
Build a balanced BST from a strictly increasing array. In each nonempty slice choose its middle element as the subtree root. If its length is even, choose the lower of its two middle elements. Recurse on the elements before and after the chosen element for its left and right subtrees.
- Input
- Read n (0 <= n <= 100), followed by n strictly increasing integers in [-1000, 1000].
- Output
- Print the preorder of the constructed tree (root, left subtree, right subtree) as space-separated keys on one line. Print EMPTY for an empty tree.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…