-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path654.py
34 lines (25 loc) · 901 Bytes
/
654.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## Leetcode 654 最大二叉树
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
if len(nums) == 1:
return TreeNode(nums[0])
maxNum = -1
maxIndex = -1
for i in range(len(nums)):
if nums[i] > maxNum:
maxNum = nums[i]
maxIndex = i
root = TreeNode(val=maxNum)
if 0 < maxIndex:
leftTree = nums[:maxIndex]
root.left = self.constructMaximumBinaryTree(leftTree)
if maxIndex < len(nums) - 1:
rightTree = nums[maxIndex + 1:]
root.right = self.constructMaximumBinaryTree(rightTree)
return root