Skip to content

Commit

Permalink
python递归法 更快
Browse files Browse the repository at this point in the history
python递归法 更快
  • Loading branch information
ORainn authored Nov 29, 2021
1 parent 126a62d commit 997ffb1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions problems/0654.最大二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""最大二叉树 递归法"""

Expand Down

0 comments on commit 997ffb1

Please sign in to comment.