Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion solutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,37 @@ tree.left.right = new BinaryTree(200);
// 8

// you'll need to create a binary search tree constructor!
var arrayToBinarySearchTree = function(array){

// constructor and methods
var BinarySearchTree = function(value){
this.value = value;
this.right = null;
this.left = null;
};

BinarySearchTree.prototype.addLeft = function(tree){
this.left = tree;
};

BinarySearchTree.prototype.addRight = function(tree){
this.right = tree;
};

// Solution:
BinarySearchTree.arrayToBST = function(array){
return (function buildTree(array, left, right){
var middle = Math.floor((right + left) / 2);
var tree = new BinarySearchTree(array[middle]);
left <= middle - 1 && tree.addLeft(buildTree(array, left, middle - 1));
right >= middle + 1 && tree.addRight(buildTree(array, middle + 1, right));
return tree;
})(array, 0, array.length - 1);
};


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

// answer to the permutations problem given in the review session.
function permutations(rounds, choices){
Expand Down