Created
August 15, 2018 21:27
-
-
Save cashlo/99b3e20b9d3860ae42e6cc13ec6c4423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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