Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1.  
  2. class TreeNode(object):
  3.   def __init__(self, x):
  4.     self.val = x
  5.     self.left = None
  6.     self.right = None
  7.  
  8. class Solution(object):
  9.     def constructMaximumBinaryTree(self, nums):
  10.         """
  11.        :type nums: List[int]
  12.        :rtype: TreeNode
  13.        """
  14.         index_of_max = 0
  15.         value_of_max = nums[0]
  16.        
  17.         for i in range(len(nums)):
  18.             if nums[i] > value_of_max:
  19.                 value_of_max = n
  20.                 index_of_max = i
  21.        
  22.         # split it on the max
  23.         left_nums = [:i]
  24.         right_nums = [i+1:]
  25.        
  26.         result = TreeNode(value_of_max)
  27.         result.left = self.constructMaximumBinaryTree(left_nums)
  28.         result.right = self.constructMaximumBinaryTree(right_nums)
  29.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement