Created
August 6, 2017 02:36
-
-
Save cixuuz/b6b1afcc362d0355ba868fab03d43c7d to your computer and use it in GitHub Desktop.
[654. Maximum Binary Tree]
This file contains hidden or 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
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