-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode-109.js
48 lines (45 loc) · 1 KB
/
Leetcode-109.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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* 109. Convert Sorted List to Binary Search Tree
* https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
* @param {ListNode} head
* @return {TreeNode}
*/
/**
* 中序遍历还原二叉树
*/
const sortedListToBST = (head) => {
let length = 0, curr = head
while(curr) { curr = curr.next; length++ }
const toBST = (left, right) => {
if (left > right) return null
let mid = (left + right) >> 1
let leftNode = toBST(left, mid-1)
let node = new TreeNode(head.val)
head = head.next
let rightNode = toBST(mid+1, right)
node.left = leftNode
node.right = rightNode
return node
}
return toBST(0, length-1)
}
/**
* 递归(分治) + 双指针
*/
/**
* 递归(分治) + 转成数组
*/