From ee99a72d18c6134c314337f7ac66f9232b9d4a61 Mon Sep 17 00:00:00 2001 From: heystone999 <52831724+heystone999@users.noreply.github.com> Date: Sat, 30 Mar 2024 15:51:43 -0400 Subject: [PATCH] =?UTF-8?q?feat(problem):=20=E6=9B=B4=E6=96=B00108.?= =?UTF-8?q?=E5=B0=86=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E3=80=82?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86Python=E9=80=92=E5=BD=92=E7=9A=84?= =?UTF-8?q?=E7=B2=BE=E7=AE=80=E7=89=88=EF=BC=8C=E8=87=AA=E8=BA=AB=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0108.将有序数组转换为二叉搜索树.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 9fa684cf..dd2245db 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -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