Balanced BST from a sorted array · Compiler

Balanced BST from a sorted array

Build a binary search tree from a sorted list of distinct keys. For each nonempty slice choose the middle key as its root, taking the smaller of the two middle keys for an even length. Apply the same rule to the keys on either side to form the left and right subtrees. Report the preorder traversal by visiting each root, then its left subtree, then its right subtree.

Input
The first line contains n (1 <= n <= 100). The second line contains n strictly increasing space-separated integers between -1000 and 1000.
Output
Print the keys in preorder on one line, separated by spaces.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…