Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2502 from heystone999/master
Browse files Browse the repository at this point in the history
feat(problem): 更新0108.将有序数组转换为二叉搜索树。添加了Python递归的精简版,自身调用
  • Loading branch information
youngyangyang04 authored Apr 21, 2024
2 parents 49acc74 + ee99a72 commit 9287b4d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions problems/0108.将有序数组转换为二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ class Solution:
return root

```
递归 精简(自身调用)
```python
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid + 1 :])
return root
```

迭代法
```python
Expand Down

0 comments on commit 9287b4d

Please sign in to comment.