-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path108.将有序数组转换为二叉搜索树.js
48 lines (43 loc) · 1.1 KB
/
108.将有序数组转换为二叉搜索树.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
/*
* @lc app=leetcode.cn id=108 lang=javascript
*
* [108] 将有序数组转换为二叉搜索树
*/
// @lc code=start
/**
* 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}
*/
// TODO
var sortedArrayToBST = function(nums) {
return build(nums, 0, nums.length - 1)
function build(nums, lo, hi) {
if (lo > hi) {
return null
}
// const mid = Math.floor((lo + hi) / 2)
const mid = lo + parseInt((hi - lo) / 2) // ❗
const root = new TreeNode(nums[mid])
root.left = build(nums, lo, mid - 1)
root.right = build(nums, mid + 1, hi)
return root
}
}
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val
this.left = left === undefined ? null : left
this.right = right === undefined ? null : right
}
console.log(
'sortedArrayToBST([-10,-3,0,5,9])',
sortedArrayToBST([-10, -3, 0, 5, 9])
)
// @lc code=end