# Definition for a binary tree node. class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: def constructMaximumBinaryTree(self, nums): if len(nums) == 0: return None elif len(nums) == 1: return TreeNode(nums[0]) else: max = -1 maxIndex = -1 for x in range(len(nums)): if nums[x] > max: max = nums[x] maxIndex = x root = TreeNode(max) root.left = self.constructMaximumBinaryTree(nums[0:maxIndex]) root.right = self.constructMaximumBinaryTree(nums[maxIndex + 1:]) return root s = Solution() print("Expected tree with root 6") print("Got: tree with root", s.constructMaximumBinaryTree([3, 2, 1, 6, 0, 5]).val)