#K44942. Convert Sorted Array to Height Balanced BST
Convert Sorted Array to Height Balanced BST
Convert Sorted Array to Height Balanced BST
Given a sorted array of distinct integers, your task is to convert it into a height balanced binary search tree (BST) with minimal height. A height balanced BST is defined as a tree in which the absolute difference in height between the left and right subtree of every node is at most 1.
After constructing the BST, perform an inorder traversal. Since the inorder traversal of a BST yields the nodes in sorted order, the output should match the input array.
This problem tests your understanding of binary trees, recursion, and efficient tree construction.
Input: A sorted array of integers.
Output: The inorder traversal of the constructed BST printed as space-separated integers.
inputFormat
The first line contains a single integer n
representing the number of elements in the sorted array. The second line contains n
space-separated integers in ascending order. If n
is 0, the second line will be empty.
outputFormat
Print the inorder traversal of the constructed BST as a sequence of space-separated integers. For an empty tree, output nothing.## sample
7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
</p>