Skip to content

Commit 55467e8

Browse files
committed
binary search tree O(n) O(n)
1 parent c0d9740 commit 55467e8

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
1-
/**
2-
* Definition for a binary tree node.
3-
* function TreeNode(val, left, right) {
4-
* this.val = (val===undefined ? 0 : val)
5-
* this.left = (left===undefined ? null : left)
6-
* this.right = (right===undefined ? null : right)
7-
* }
8-
*/
9-
/**
10-
* @param {number[]} nums
11-
* @return {TreeNode}
12-
*/
13-
// p: array
14-
// r: bi search tree
15-
// 0 3 4
16-
// j
17-
// i j r
18-
// i j i j
19-
// [-10,-3,0,5,9] Math.floor(i+j/2 = 2.5) = 2 => root
20-
// 0 3
21-
// [1,3,4,6] => root i
22-
//j r ij
23-
// [1,3] root = 1
24-
251
var sortedArrayToBST = function (nums, i = 0, j = nums.length - 1) {
262
if (i > j) return null;
27-
const rootIndex = Math.floor((i + j) / 2);
28-
const root = new TreeNode(nums[rootIndex]);
29-
root.left = sortedArrayToBST(nums, i, rootIndex - 1);
30-
root.right = sortedArrayToBST(nums, rootIndex + 1, j);
3+
const mid = Math.floor((i + j) / 2);
4+
const rootVal = nums[mid];
5+
const root = new TreeNode(rootVal);
6+
7+
root.left = sortedArrayToBST(nums, i, mid - 1);
8+
root.right = sortedArrayToBST(nums, mid + 1, j);
9+
3110
return root;
3211
};
3312

34-
// var sortedArrayToBST = function(nums) {
35-
// if (nums.length === 0) return null;
36-
// const rootIndex = Math.floor(nums.length / 2)
37-
// const root = new TreeNode(nums[rootIndex]);
38-
// root.left = sortedArrayToBST(nums.slice(0, rootIndex));
39-
// root.right = sortedArrayToBST(nums.slice(rootIndex + 1));
40-
// return root;
13+
// /**
14+
// * Definition for a binary tree node.
15+
// * function TreeNode(val, left, right) {
16+
// * this.val = (val===undefined ? 0 : val)
17+
// * this.left = (left===undefined ? null : left)
18+
// * this.right = (right===undefined ? null : right)
19+
// * }
20+
// */
21+
// /**
22+
// * @param {number[]} nums
23+
// * @return {TreeNode}
24+
// */
25+
// // p: array
26+
// // r: bi search tree
27+
// // 0 3 4
28+
// // j
29+
// // i j r
30+
// // i j i j
31+
// // [-10,-3,0,5,9] Math.floor(i+j/2 = 2.5) = 2 => root
32+
// // 0 3
33+
// // [1,3,4,6] => root i
34+
// //j r ij
35+
// // [1,3] root = 1
36+
37+
// var sortedArrayToBST = function (nums, i = 0, j = nums.length - 1) {
38+
// if (i > j) return null;
39+
// const rootIndex = Math.floor((i + j) / 2);
40+
// const root = new TreeNode(nums[rootIndex]);
41+
// root.left = sortedArrayToBST(nums, i, rootIndex - 1);
42+
// root.right = sortedArrayToBST(nums, rootIndex + 1, j);
43+
// return root;
4144
// };
45+
46+
// // var sortedArrayToBST = function(nums) {
47+
// // if (nums.length === 0) return null;
48+
// // const rootIndex = Math.floor(nums.length / 2)
49+
// // const root = new TreeNode(nums[rootIndex]);
50+
// // root.left = sortedArrayToBST(nums.slice(0, rootIndex));
51+
// // root.right = sortedArrayToBST(nums.slice(rootIndex + 1));
52+
// // return root;
53+
// // };

0 commit comments

Comments
 (0)