forked from jmcilhargey/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchpt4-minimal-tree.js
More file actions
24 lines (21 loc) · 940 Bytes
/
chpt4-minimal-tree.js
File metadata and controls
24 lines (21 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
// We're given a sorted array and our task is to create a BST with minimal height
// That means as many nodes to the left as right so it's balanced
function makeBinaryTreeMinimalHeight(sortedArray, startIndex, endIndex) {
var midPoint = Math.floor((endIndex - startIndex) / 2 + startIndex);
var currentNode = new Node(sortedArray[midPoint]);
// Base case where we've ran out of values in array
if (startIndex > endIndex) {
return null;
}
// Recurse by cutting the array range in half for left and right sub-trees
currentNode.left = makeBinaryTreeMinimalHeight(sortedArray, startIndex, midPoint - 1);
currentNode.right = makeBinaryTreeMinimalHeight(sortedArray, midPoint + 1, endIndex);
return currentNode;
}
var arr = [1, 4, 6, 9, 13, 16, 19, 20, 24, 27];
var bst = makeBinaryTreeMinimalHeight(arr, 0, arr.length - 1);