From 5bf54160131703bff0029272f98626d8717bf41f Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:40:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md=20=E6=B7=BB=E5=8A=A0=E6=99=AE=E9=80=9A=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=88=A0=E9=99=A4=E6=96=B9=E5=BC=8F?= =?UTF-8?q?python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...255\347\232\204\350\212\202\347\202\271.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index facdb2f938..0655f8c5d0 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -348,6 +348,24 @@ class Solution: return root ``` +**普通二叉树的删除方式** +```python +class Solution: + def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: + if not root: return root + if root.val == key: + if not root.right: # 这里第二次操作目标值:最终删除的作用 + return root.left + tmp = root.right + while tmp.left: + tmp = tmp.left + root.val, tmp.val = tmp.val, root.val # 这里第一次操作目标值:交换目标值其右子树最左面节点。 + + root.left = self.deleteNode(root.left, key) + root.right = self.deleteNode(root.right, key) + return root +``` + **迭代法** ```python class Solution: From cbc75c008452a9ac0de0b580a30a4a1d9ec969b5 Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:47:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index ac36509f21..1fd6fce098 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -299,6 +299,32 @@ class Solution: return root ``` +**迭代** +```python +class Solution: + def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: + if not root: return root + # 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开 + while root and (root.val < low or root.val > high): + if root.val < low: # 小于L往右走 + root = root.right + else: # 大于R往左走 + root = root.left + # 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况 + cur = root + while cur: + while cur.left and cur.left.val < low: + cur.left = cur.left.right + cur = cur.left + # 此时root已经在[L, R] 范围内,处理右孩子大于R的情况 + cur = root + while cur: + while cur.right and cur.right.val > high: + cur.right = cur.right.left + cur = cur.right + return root +``` + ## Go ```go From f96384951fb35dc196beea26151ab992a551b08e Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Thu, 19 Jan 2023 18:00:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200108.=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91.md=20=E6=B7=BB=E5=8A=A0=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index c75f87d1e0..f0455a834f 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -352,6 +352,38 @@ class Solution: return mid_root ``` +**迭代**(左闭右开) +```python +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: + if len(nums) == 0: return None + root = TreeNode() # 初始化 + nodeSt = [root] + leftSt = [0] + rightSt = [len(nums)] + + while nodeSt: + node = nodeSt.pop() # 处理根节点 + left = leftSt.pop() + right = rightSt.pop() + mid = left + (right - left) // 2 + node.val = nums[mid] + + if left < mid: # 处理左区间 + node.left = TreeNode() + nodeSt.append(node.left) + leftSt.append(left) + rightSt.append(mid) + + if right > mid + 1: # 处理右区间 + node.right = TreeNode() + nodeSt.append(node.right) + leftSt.append(mid + 1) + rightSt.append(right) + + return root +``` + ## Go 递归(隐含回溯)