欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

2020-07-03-每日一题

程序员文章站 2022-09-13 22:53:21
更多python分类刷题题解代码:请参考github,博客, 知乎108. 将有序数组转换为二叉搜索树https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left...

更多python分类刷题题解代码:请参考github,博客, 知乎

108. 将有序数组转换为二叉搜索树

https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:

        def helper(nums):
            if not nums: return 
            l = len(nums)
            mid = l // 2
            node = TreeNode(nums[mid])
            node.left = helper(nums[:mid])
            node.right = helper(nums[mid+1:])
            return node
        
        return helper(nums)

更多python分类刷题题解代码:请参考github,博客, 知乎

更多技术文章请点击查看

本文地址:https://blog.csdn.net/lxztju/article/details/107134834