mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -256,6 +256,23 @@ class Solution {
|
||||
## Python
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
"""递归法 更快"""
|
||||
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
|
||||
if not nums:
|
||||
return None
|
||||
maxvalue = max(nums)
|
||||
index = nums.index(maxvalue)
|
||||
|
||||
root = TreeNode(maxvalue)
|
||||
|
||||
left = nums[:index]
|
||||
right = nums[index + 1:]
|
||||
|
||||
root.left = self.constructMaximumBinaryTree(left)
|
||||
root.right = self.constructMaximumBinaryTree(right)
|
||||
return root
|
||||
|
||||
class Solution:
|
||||
"""最大二叉树 递归法"""
|
||||
|
||||
|
Reference in New Issue
Block a user