用js刷leetcode时的一些想法及解题思路
Description
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
solution1:
var sortedListToBST = function(head) {
return array2BST(linked2Array(head))
};
function linked2Array(head, result = []){ //把链表转成数组
while(true){
if(!head){
break
} else {
result.push(head.val)
head = head.next
}
}
return result
}
function array2BST(array, root = new TreeNode()){//把数组转成二叉树
if(!array.length){
return null
} else{
var mid = array.length / 2 | 0
root.val = array[mid]
root.left = array2BST(array.slice(0, mid))
root.right = array2BST(array.slice(mid + 1))
}
return root
}
solution2
/**
* 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;
* }
*/
/**
* @param {ListNode} head
* @return {TreeNode}
*/
var sortedListToBST = function(head, tail) {
if(!head) return null;
var slow = head, fast = head;
while(fast != tail){
fast = fast.next;
if(fast != tail){
fast = fast.next;
slow = slow.next;
}
}
var node = new TreeNode(slow.val);
node.left = head == slow ? null : sortedListToBST(head, slow);
node.right = slow.next == tail ? null : sortedListToBST(slow.next, tail);
return node;
};
Done.