forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0108-convert-sorted-array-to-binary-search-tree.js
89 lines (69 loc) · 2.77 KB
/
0108-convert-sorted-array-to-binary-search-tree.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* 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)
* }
*/
/**
* DFS - Preorder | Left as mid
* Time O(N) | Space O(log(N))
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = (nums, left = 0, right = (nums.length - 1)) => {
const isBaseCase = (right < left);
if (isBaseCase) return null;
return dfs(nums, left, right);/* Time O(N) | Space O(log(N)) */
};
var dfs = (nums, left, right) => {
const mid = (left + right) >> 1;
const root = new TreeNode(nums[mid]); /* | Ignore Auxillary Space O(N) */
root.left = sortedArrayToBST(nums, left, (mid - 1)); /* Time O(N) | Space O(log(N)) */
root.right = sortedArrayToBST(nums, (mid + 1), right);/* Time O(N) | Space O(log(N)) */
return root;
}
/**
* DFS - Preorder | Right as mid
* Time O(N) | Space O(log(N))
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = (nums, left = 0, right = (nums.length - 1)) => {
const isBaseCase = (right < left);
if (isBaseCase) return null;
return dfs(nums, left, right);/* Time O(N) | Space O(log(N)) */
};
var dfs = (nums, left, right) => {
let mid = (left + right) >> 1;
const isOdd = (((left + right) % 2) === 1);
if (isOdd) mid += 1;
const root = new TreeNode(nums[mid]); /* | Ignore Auxillary Space O(N) */
root.left = sortedArrayToBST(nums, left, (mid - 1)); /* Time O(N) | Space O(log(N)) */
root.right = sortedArrayToBST(nums, (mid + 1), right);/* Time O(N) | Space O(log(N)) */
return root;
}
/**
* DFS - Preorder | Random as mid
* Time O(N) | Space O(log(N))
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = (nums, left = 0, right = (nums.length - 1)) => {
const isBaseCase = (right < left);
if (isBaseCase) return null;
return dfs(nums, left, right);/* Time O(N) | Space O(log(N)) */
};
var dfs = (nums, left, right) => {
let mid = (left + right) >> 1;
const isOdd = (((left + right) % 2) === 1);
if (isOdd) mid += Math.floor(Math.random() * 2);
const root = new TreeNode(nums[mid]); /* | Ignore Auxillary Space O(N) */
root.left = sortedArrayToBST(nums, left, (mid - 1)); /* Time O(N) | Space O(log(N)) */
root.right = sortedArrayToBST(nums, (mid + 1), right);/* Time O(N) | Space O(log(N)) */
return root;
}