mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 05:20:59 +08:00
Update 0654.最大二叉树.md
This commit is contained in:
@ -259,52 +259,70 @@ class Solution {
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
(版本一) 基础版
|
||||
```python
|
||||
# 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]) -> 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
|
||||
if len(nums) == 1:
|
||||
return TreeNode(nums[0])
|
||||
node = TreeNode(0)
|
||||
# 找到数组中最大的值和对应的下标
|
||||
maxValue = 0
|
||||
maxValueIndex = 0
|
||||
for i in range(len(nums)):
|
||||
if nums[i] > maxValue:
|
||||
maxValue = nums[i]
|
||||
maxValueIndex = i
|
||||
node.val = maxValue
|
||||
# 最大值所在的下标左区间 构造左子树
|
||||
if maxValueIndex > 0:
|
||||
new_list = nums[:maxValueIndex]
|
||||
node.left = self.constructMaximumBinaryTree(new_list)
|
||||
# 最大值所在的下标右区间 构造右子树
|
||||
if maxValueIndex < len(nums) - 1:
|
||||
new_list = nums[maxValueIndex+1:]
|
||||
node.right = self.constructMaximumBinaryTree(new_list)
|
||||
return node
|
||||
|
||||
```
|
||||
(版本二) 使用下标
|
||||
class Solution:
|
||||
"""最大二叉树 递归法"""
|
||||
def traversal(self, nums: List[int], left: int, right: int) -> TreeNode:
|
||||
if left >= right:
|
||||
return None
|
||||
maxValueIndex = left
|
||||
for i in range(left + 1, right):
|
||||
if nums[i] > nums[maxValueIndex]:
|
||||
maxValueIndex = i
|
||||
root = TreeNode(nums[maxValueIndex])
|
||||
root.left = self.traversal(nums, left, maxValueIndex)
|
||||
root.right = self.traversal(nums, maxValueIndex + 1, right)
|
||||
return root
|
||||
|
||||
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
|
||||
return self.traversal(nums, 0, len(nums))
|
||||
|
||||
def traversal(self, nums: List[int], begin: int, end: int) -> TreeNode:
|
||||
# 列表长度为0时返回空节点
|
||||
if begin == end:
|
||||
return None
|
||||
|
||||
# 找到最大的值和其对应的下标
|
||||
max_index = begin
|
||||
for i in range(begin, end):
|
||||
if nums[i] > nums[max_index]:
|
||||
max_index = i
|
||||
|
||||
# 构建当前节点
|
||||
root = TreeNode(nums[max_index])
|
||||
|
||||
# 递归构建左右子树
|
||||
root.left = self.traversal(nums, begin, max_index)
|
||||
root.right = self.traversal(nums, max_index + 1, end)
|
||||
|
||||
return root
|
||||
```
|
||||
(版本三) 使用切片
|
||||
class Solution:
|
||||
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
|
||||
if not nums:
|
||||
return None
|
||||
max_val = max(nums)
|
||||
max_index = nums.index(max_val)
|
||||
node = TreeNode(max_val)
|
||||
node.left = self.constructMaximumBinaryTree(nums[:max_index])
|
||||
node.right = self.constructMaximumBinaryTree(nums[max_index+1:])
|
||||
return node
|
||||
|
||||
```
|
||||
### Go
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user