Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Given a sorted array, write an algorithm to create a binary search tree with minimal height.
function createMinimalBST(array, start, end){
if (end < start){
return null;
var mid = Math.floor( (start + end) / 2 );
var node = {val: array[mid], left: null, right: null};
node.left = createMinimalBST(array, start, mid-1);
node.right = createMinimalBST(array, mid+1, end);
return node;
// array1 = [1,2,3,4,5,6,7,8,9];
// var result = createMinimalBST(array1, 0, array1.length-1);
// console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.