-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
38 lines (35 loc) · 945 Bytes
/
index.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
const { TreeNode } = require('../utils');
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function(nums) {
if (nums.length === 0) {
return null;
}
return dfs(0, nums.length - 1);
function dfs(left, right) {
if (left > right) {
return null;
}
const middle = Math.floor(left + (right - left) / 2);
const node = new TreeNode(nums[middle]);
node.left = dfs(left, middle - 1);
node.right = dfs(middle + 1, right);
return node;
}
};
const result = sortedArrayToBST([-10,-3,0,5,9]);
module.exports = {
title:'Convert Sorted Array to Binary Search Tree',
url:'https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/',
id:'108',
difficulty:'easy',
};