-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertSortedArrayToBinarySearchTree.ts
More file actions
46 lines (36 loc) · 1.17 KB
/
Copy pathconvertSortedArrayToBinarySearchTree.ts
File metadata and controls
46 lines (36 loc) · 1.17 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
import { TreeNode } from './types/tree';
function sortedArrayToBST(nums: number[]): TreeNode | null {
const mid = Math.floor(nums.length / 2);
const root = new TreeNode(nums[mid]);
const queue: Array<Array<TreeNode | Array<number>>> = [
[root, [0, mid - 1]],
[root, [mid + 1, nums.length - 1]]
];
while (queue.length > 0) {
const [parent, positions] = queue.shift();
const [left, right] = positions as Array<number>;
if (left <= right && parent !== null) {
const subMidLength = Math.floor((left + right) / 2);
const child = new TreeNode(nums[subMidLength]);
if (nums[subMidLength] > (parent as TreeNode)?.val) {
(parent as TreeNode).right = child;
} else {
(parent as TreeNode).left = child;
}
queue.push([child, [left, subMidLength - 1]]);
queue.push([child, [subMidLength + 1, right]]);
}
}
return root;
}
// Recursion
function sortedArrayToBSTV2(nums: number[]): TreeNode | null {
if (nums.length === 0) return null;
const center = Math.floor(nums.length / 2);
return new TreeNode(
nums[center],
sortedArrayToBST(nums.slice(0, center)),
sortedArrayToBST(nums.slice(center + 1))
);
}
console.log(sortedArrayToBST([1, 3]));