二叉树TreeNode()的使用
它是怎么存放的呢?例如:力扣108将有序数列转化为二叉树
class Solution(object):
def sortedArrayToBST(self, nums):
"""
:type nums: List
:rtype: TreeNode
"""
if not nums:
return None
length = len(nums)
mid = length // 2
root = TreeNode(nums)
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums)
return root
页:
[1]