File tree Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -233,7 +233,27 @@ class Solution {
233
233
```
234
234
235
235
Python:
236
-
236
+ ``` python3
237
+ # Definition for a binary tree node.
238
+ # class TreeNode:
239
+ # def __init__(self, val=0, left=None, right=None):
240
+ # self.val = val
241
+ # self.left = left
242
+ # self.right = right
243
+ # 递归法
244
+ class Solution :
245
+ def sortedArrayToBST (self , nums : List[int ]) -> TreeNode:
246
+ def buildaTree (left ,right ):
247
+ if left > right: return None # 左闭右闭的区间,当区间 left > right的时候,就是空节点,当left = right的时候,不为空
248
+ mid = left + (right - left) // 2 # 保证数据不会越界
249
+ val = nums[mid]
250
+ root = TreeNode(val)
251
+ root.left = buildaTree(left,mid - 1 )
252
+ root.right = buildaTree(mid + 1 ,right)
253
+ return root
254
+ root = buildaTree(0 ,len (nums) - 1 ) # 左闭右闭区间
255
+ return root
256
+ ```
237
257
238
258
Go:
239
259
244
264
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
245
265
* B站视频:[ 代码随想录] ( https://space.bilibili.com/525438321 )
246
266
* 知识星球:[ 代码随想录] ( https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ )
247
- <div align="center"><img src=../pics/公众号.png width=450 alt=> </img ></div >
267
+ <div align="center"><img src=../pics/公众号.png width=450 alt=> </img ></div >
You can’t perform that action at this time.
0 commit comments