-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayToBst.js
More file actions
24 lines (21 loc) · 808 Bytes
/
arrayToBst.js
File metadata and controls
24 lines (21 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Given an array where elements are sorted in ascending order, convert
// it to a height balanced BST. For this problem, a height-balanced binary
// tree is defined as a binary tree in which the depth of the two subtrees
// of every node never differ by more than 1.
const sortedArrayToBST = nums => {
if (!nums || nums.length < 1) return null
let root = new TreeNode()
const traverse = (minIdx, maxIdx, node) => {
if (minIdx <= maxIdx) {
let mid = Math.ceil((maxIdx - minIdx) / 2 + minIdx)
node.val = nums[mid]
node.left = traverse(minIdx, mid - 1, new TreeNode())
node.right = traverse(mid + 1, maxIdx, new TreeNode())
return node
} else {
return null
}
}
traverse(0, nums.length - 1, root)
return root
}