-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108-convert-sorted-array-to-binary-search-tree.js
More file actions
56 lines (46 loc) · 1.4 KB
/
108-convert-sorted-array-to-binary-search-tree.js
File metadata and controls
56 lines (46 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function (nums) {
console.log("------".repeat(20));
console.log("nums", nums);
function toBST(start, end, lado) {
console.log("------".repeat(10));
console.log("start", start, "end", end, lado);
if (start > end) {
console.log("retornamos xq start es mayor que end");
return null;
}
// console.log("start", start, "end", end);
// conviene hacer .ceil
let mid = Math.ceil((start + end) / 2);
console.log("mid", mid);
const root = new TreeNode(nums[mid]);
const l = toBST(start, mid - 1, "left");
// mid = Math.floor((start + end) / 2);
// console.log("mid (floor)", mid);
const r = toBST(mid + 1, end, "right");
root.left = l;
root.right = r;
return root;
}
// console.log("nums.length - 1", nums.length - 1);
return toBST(0, nums.length - 1, "root");
};
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
// Case 1
const arr = [-10, -3, 0, 5, 9];
console.log(sortedArrayToBST(arr));