File tree 2 files changed +8
-11
lines changed
2 files changed +8
-11
lines changed Original file line number Diff line number Diff line change 11
11
# self.right = right
12
12
class Solution:
13
13
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
14
- def helper(low, high):
15
- if low > high:
16
- return None
17
- mid = (low+high)//2
18
- root = TreeNode(nums[mid])
19
- root.left = helper(low, mid-1)
20
- root.right = helper(mid+1, high)
21
- return root
22
-
23
- return helper(0, len(nums)-1)
14
+ if not nums:
15
+ return None
16
+ mid = len(nums)//2
17
+ root = TreeNode(nums[mid])
18
+ root.left = self.sortedArrayToBST(nums[:mid])
19
+ root.right = self.sortedArrayToBST(nums[mid+1:])
20
+ return root
24
21
```
Original file line number Diff line number Diff line change @@ -104,11 +104,11 @@ LeetCode 精选算法题, Python题解+详细注释,持续更新。
104
104
- [ 82. 二叉树的最近公共祖先] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/82.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.md )
105
105
- [ 90. 从头到尾打印列表] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/90.%20%E4%BB%8E%E5%A4%B4%E5%88%B0%E5%B0%BE%E6%89%93%E5%8D%B0%E5%88%97%E8%A1%A8.md )
106
106
- [ 97. 二叉搜索树的后序遍历序列] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/97.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97.md )
107
+ - [ 37. 将有序数组转换为二叉搜索树] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/37.%20%E5%B0%86%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.md )
107
108
- [ 94. 24 点游戏] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/94.%2024%20%E7%82%B9%E6%B8%B8%E6%88%8F.md )
108
109
- [ 77. 平衡二叉树] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/77.%20%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.md )
109
110
- [ 81. 二叉树的直径] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/81.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%9B%B4%E5%BE%84.md )
110
111
- [ 46. 二叉树中的最大路径和] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/46.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E8%B7%AF%E5%BE%84%E5%92%8C.md )
111
- - [ 37. 将有序数组转换为二叉搜索树] ( https://github.com/jasoncao11/Algorithm-notebook/blob/master/37.%20%E5%B0%86%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.md )
112
112
113
113
### 8. 二分法
114
114
You can’t perform that action at this time.
0 commit comments