Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 6, 2017 02:36
Show Gist options
  • Save cixuuz/b6b1afcc362d0355ba868fab03d43c7d to your computer and use it in GitHub Desktop.
Save cixuuz/b6b1afcc362d0355ba868fab03d43c7d to your computer and use it in GitHub Desktop.
[654. Maximum Binary Tree]
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if len(nums) == 0:
return None
val = max(nums)
max_idx = nums.index(val)
leftNode = self.constructMaximumBinaryTree(nums[:max_idx])
rightNode = self.constructMaximumBinaryTree(nums[max_idx+1:])
node = TreeNode(val)
node.left = leftNode
node.right = rightNode
return node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment