Maximum Binary Tree LeetCode solution in Python

1
class Solution:
2
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
3
if not nums:
4
return None
5
m = max(nums)
6
idx = nums.index(m)
7
root = TreeNode(m)
8
root.left = self.constructMaximumBinaryTree(nums[:idx])
9
root.right = self.constructMaximumBinaryTree(nums[idx + 1 :])
10
return root

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0