From 12eb9158c3b1e7148add297d289176d4543cfb15 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Wed, 12 Oct 2022 10:19:54 -0600 Subject: [PATCH 1/4] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0701.二叉搜索树中的插入操作.md python3 迭代法优化 --- problems/0701.二叉搜索树中的插入操作.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 06e1c88f..599dd073 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -337,16 +337,15 @@ class Solution: def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: if not root: return TreeNode(val) - parent = None + parent = None # 此步可以省略 cur = root # 用while循环不断地找新节点的parent while cur: + parent = cur # 首先保存当前非空节点作为下一次迭代的父节点 if cur.val < val: - parent = cur cur = cur.right elif cur.val > val: - parent = cur cur = cur.left # 运行到这意味着已经跳出上面的while循环, From f6db9d06e23986ea946d5108e2a921d261f1ef91 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Wed, 12 Oct 2022 10:34:56 -0600 Subject: [PATCH 2/4] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0701.二叉搜索树中的插入操作.md Python3 **递归法** - 无返回值 有注释 不用Helper function --- .../0701.二叉搜索树中的插入操作.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 599dd073..6a78b2b4 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -330,6 +330,27 @@ class Solution: return root ``` +**递归法** - 无返回值 有注释 不用Helper function +``` +class Solution: + last = None + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if not root: # for root==None + return TreeNode(val) + if root.valval: + if root.left==None: # found the parent + root.left = TreeNode(val) + else: # not found, keep searching + self.insertIntoBST(root.left, val) + # return the final tree + return root +``` + **迭代法** 与无返回值的递归函数的思路大体一致 ```python From abb348dbc4dc6f0081ae313c23c1e3a408dfd818 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Wed, 12 Oct 2022 10:38:50 -0600 Subject: [PATCH 3/4] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0701.二叉搜索树中的插入操作.md Python3 **递归法** - 无返回值 有注释 不用Helper function - 删掉无用变量 增加python标志 --- problems/0701.二叉搜索树中的插入操作.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 6a78b2b4..a6f932b0 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -331,9 +331,8 @@ class Solution: ``` **递归法** - 无返回值 有注释 不用Helper function -``` +```python class Solution: - last = None def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: if not root: # for root==None return TreeNode(val) From bd77b3bb63445b8ab09d47a95ab7e4e07433b307 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Wed, 12 Oct 2022 14:07:50 -0600 Subject: [PATCH 4/4] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0450.删除二叉搜索树中的节点.md 添加python3迭代法 --- .../0450.删除二叉搜索树中的节点.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 2d6d4ef2..541c8be4 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -344,6 +344,48 @@ class Solution: return root ``` +**迭代法** +```python +class Solution: + def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: + # 找到节点后分两步,1. 把节点的左子树和右子树连起来,2. 把右子树跟父节点连起来 + # root is None + if not root: return root + p = root + last = None + while p: + if p.val==key: + # 1. connect left to right + # right is not None -> left is None | left is not None + if p.right: + if p.left: + node = p.right + while node.left: + node = node.left + node.left = p.left + right = p.right + else: + # right is None -> right=left + right = p.left + # 2. connect right to last + if last==None: + root = right + elif last.val>key: + last.left = right + else: + last.right = right + # 3. return + break + else: + # Update last and continue + last = p + if p.val>key: + p = p.left + else: + p = p.right + return root +``` + ## Go ```Go // 递归版本