leetcode 109. Convert Sorted List to Binary Search Tree 发表于 2018-12-07 | 分类于 leetcode 题目链接:leetcode 思路:把链表转成数组,再用递归生成树。 12345678910111213141516171819202122232425262728293031# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = None# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def get_midnode(self,arr): if not arr:return None mid = len(arr)//2 node = TreeNode(arr[mid]) node.left = self.get_midnode(arr[:mid]) node.right = self.get_midnode(arr[mid+1:]) return node def sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ node,arr=head, [] while node: arr.append(node.val) node = node.next return self.get_midnode(arr)