From aad6b8cee27afa237dca3997fa720705410eac7a Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 8 Jan 2023 16:37:47 -0600 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BA=86python=20version?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E3=80=82=E5=81=9A=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=E7=AE=80=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 2 +- ...38.把二叉搜索树转换为累加树.md | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 406242a7..28425281 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -23,7 +23,7 @@ * 111.二叉树的最小深度 -![我要打十个](https://tva1.sinaimg.cn/large/008eGmZEly1gnadnltbpjg309603w4qp.gif) +![我要打十个](https://tva1.sinaimg.cn/large/008eGmZEly1gPnadnltbpjg309603w4qp.gif) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index ec12c525..d2e98dc8 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -209,29 +209,28 @@ class Solution { # self.right = right class Solution: def __init__(self): - self.pre = TreeNode() + self.count = 0 def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root == None: + return ''' 倒序累加替换: - [2, 5, 13] -> [[2]+[1]+[0], [2]+[1], [2]] -> [20, 18, 13] ''' - self.traversal(root) - return root - - def traversal(self, root: TreeNode) -> None: - # 因为要遍历整棵树,所以递归函数不需要返回值 - # Base Case - if not root: - return None - # 单层递归逻辑:中序遍历的反译 - 右中左 - self.traversal(root.right) # 右 + # 右 + self.convertBST(root.right) + # 中 # 中节点:用当前root的值加上pre的值 - root.val += self.pre.val # 中 - self.pre = root + self.count += root.val - self.traversal(root.left) # 左 + root.val = self.count + + # 左 + self.convertBST(root.left) + + return root + ``` ## Go