Skip to content

Commit 61c8911

Browse files
committed
Added JSDoc for ch4-2.
1 parent 8a74774 commit 61c8911

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/chapter4/ch4-q02.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ function add(tree, values, start, end) {
1313
}
1414
}
1515

16+
/**
17+
* As the list is already sorted the best way to create a balanced tree is by
18+
* adding the middle node (parent) then the children. The algorithm is basically
19+
* involves adding the middle element of which split of the array so that the
20+
* parent is added before the left and right children of each subtree.
21+
*
22+
* N = |values|
23+
* Time: O(N lg N)
24+
* Additional space: O(N)
25+
*/
1626
export function makeBalancedTree(values) {
1727
let tree = new Tree();
1828
if (values && values.length) {

0 commit comments

Comments
 (0)