Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height

Sharing is caring!

Problem Statement
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.

Solution
We will try to create a binary tree such that for each node, the number of nodes in the left subtree and the right subtree are equal, if possible

Algorithm:
1. Insert into the tree the middle element of the array
2. Insert (into the left subtree) the left subarray elements
3. Insert (into the right subtree) the right subarray elements
4. Recurse

Full Code