207 篇文章 4 订阅

# 题目

109. Convert Sorted List to Binary Search Tree

Given the head of a singly linked list 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.

Example 1:

Input: head = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]


Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
Example 2:

Input: head = []
Output: []


Example 3:

Input: head = [0]
Output: [0]


Example 4:

Input: head = [1,3]
Output: [3,1]


Constraints:

• The number of nodes in head is in the range [0, 2 * 10^4].
• -10^5 <= Node.val <= 10^5

# 解法一：转为数组后，根据数组序号二分组装成二叉排序树

1. 把链表的值，组装成数组；
2. 根据数组的索引，递归调用组装成二叉排序树
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class ConvertSortedListToBinarySearchTree:
# Convert the given linked list to an arry
vals = []
return vals

def sortedListToBST(self, head: ListNode) -> TreeNode:
# check edge
return None

# Form an array out of the given linked list and then
# use the array to form the BST

# l and r represent the start and end of the given array
def convertListToBST(l, r):
# Invalid case
if l > r:
return None
# Middle element forms the root.
mid = (l + r) // 2
node = TreeNode(values[mid])

# Baes case for when there is only one element left in the array
if l == r:
return node
# Recursively fom BST on the two halves
node.left = convertListToBST(l, mid - 1)
node.right = convertListToBST(mid + 1, r)
return node

return convertListToBST(0, len(values) - 1)


# 解法二：找链表中的中间节点，组装成二叉排序树

# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class ConvertSortedListToBinarySearchTree:
# The pointer used to disconnect the left half from the mid node.
preNode = None

# Iterate until fastNode doesn't reach the end of the linked list.
while fastNode and fastNode.next:
preNode = slowNode
slowNode = slowNode.next
fastNode = fastNode.next.next

# Handling the case when slowNode was equal to head
if preNode:
preNode.next = None

return slowNode

# If the head doesn't exist, then the linked list is empty
return None

# Find the middle element for the list

# the mid becones the root of the BST.
node = TreeNode(mid.val)

# Base case when there is just one element in the linked list
return node

# Recursively form balanced BSTs using the left and right halves of the original list.
node.right = self.sortedListToBST2(mid.next)
return node



# 参考

https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/solution/

• 0
点赞
• 0
收藏 更改收藏夹
• 打赏
• 0
评论
08-18 176
11-17 142
02-05 440
05-15 1241
03-26 810
04-02 3904
07-09 195
09-20 1639
04-28 202
07-05 438
09-12 431
07-23 440
03-05 322

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

AI架构师易筋

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。