当前位置: 移动技术网 > IT编程>脚本编程>Python > 2020-07-03-每日一题

2020-07-03-每日一题

2020年07月07日  | 移动技术网IT编程  | 我要评论

更多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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网