0%
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
|
class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: if len(nums)==1: return TreeNode(val=nums[0]) maxindex,maxValue = 0,0 for index in range(0,len(nums)): if nums[index]>maxValue: maxValue = nums[index] maxindex = index print(maxValue) root = TreeNode(val=maxValue) if maxindex>0: left_nums = nums[:maxindex] root.left = self.constructMaximumBinaryTree(left_nums) if maxindex<len(nums)-1: right_nums = nums[maxindex+1:] root.right = self.constructMaximumBinaryTree(right_nums) return root
|