Skip to content

Instantly share code, notes, and snippets.

@ionox0
Created March 19, 2014 18:59
Show Gist options
  • Save ionox0/9648813 to your computer and use it in GitHub Desktop.
Save ionox0/9648813 to your computer and use it in GitHub Desktop.
converts a sorted array to a binary tree
function arrayToBST(list){
function buildTree(list, min, max){
if (min == max){
return {
value: list[max],
left: null,
right: null
};
}
console.log('asdf');
var middle = Math.floor((min+max)/2);
var left = buildTree(list, min, middle-1);
var right = buildTree(list, middle+1, max);
return {
value: list[middle],
left: left,
right: right
};
}
return buildTree(list, 0, list.length-1);
}
var test = [1,2,3,4,5,6,7];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment