-
Notifications
You must be signed in to change notification settings - Fork 0
/
109.py
28 lines (27 loc) · 894 Bytes
/
109.py
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
# 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 Solution:
def list_2_bst(self, nums):
if not nums:
return None
len_ = len(nums)
index = int(len_ / 2)
node = TreeNode(val=nums[index])
node.left = self.list_2_bst(nums=nums[:index])
node.right = self.list_2_bst(nums=nums[index + 1:])
return node
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
nums = list()
while head:
nums.append(head.val)
head = head.next
return self.list_2_bst(nums=nums)