Skip to content

Instantly share code, notes, and snippets.

@cashlo
Created August 15, 2018 21:27
Show Gist options
  • Save cashlo/99b3e20b9d3860ae42e6cc13ec6c4423 to your computer and use it in GitHub Desktop.
Save cashlo/99b3e20b9d3860ae42e6cc13ec6c4423 to your computer and use it in GitHub Desktop.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums: return None
max_num = max(nums)
max_index = nums.index(max_num)
node = TreeNode(max_num)
node.left = self.constructMaximumBinaryTree(nums[:max_index])
node.right = self.constructMaximumBinaryTree(nums[max_index+1:])
return node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment