My LeetCode grinding. Trying to do a problem a day.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.py 899B

1234567891011121314151617181920212223242526272829
  1. # Definition for a binary tree node.
  2. class TreeNode:
  3. def __init__(self, x):
  4. self.val = x
  5. self.left = None
  6. self.right = None
  7. class Solution:
  8. def constructMaximumBinaryTree(self, nums):
  9. if len(nums) == 0:
  10. return None
  11. elif len(nums) == 1:
  12. return TreeNode(nums[0])
  13. else:
  14. max = -1
  15. maxIndex = -1
  16. for x in range(len(nums)):
  17. if nums[x] > max:
  18. max = nums[x]
  19. maxIndex = x
  20. root = TreeNode(max)
  21. root.left = self.constructMaximumBinaryTree(nums[0:maxIndex])
  22. root.right = self.constructMaximumBinaryTree(nums[maxIndex + 1:])
  23. return root
  24. s = Solution()
  25. print("Expected tree with root 6")
  26. print("Got: tree with root", s.constructMaximumBinaryTree([3, 2, 1, 6, 0, 5]).val)