From a87facfca6977432a624cf4ace214558025eaefa Mon Sep 17 00:00:00 2001 From: benignbemine Date: Wed, 3 Jun 2015 13:19:31 -0700 Subject: [PATCH] added ArrayToBST solution --- solutions.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/solutions.js b/solutions.js index 9b7c15f..484b8c2 100644 --- a/solutions.js +++ b/solutions.js @@ -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){