From b35688b7808bad269c2ef75e1b57f1a1fbe7e919 Mon Sep 17 00:00:00 2001 From: Joshua <47053655+Joshua-Lu@users.noreply.github.com> Date: Fri, 14 May 2021 00:59:49 +0800 Subject: [PATCH] =?UTF-8?q?Update=200538.=E6=8A=8A=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA=E7=B4=AF?= =?UTF-8?q?=E5=8A=A0=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0538.把二叉搜索树转换为累加树 Java版本 --- ...38.把二叉搜索树转换为累加树.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index a5f4c43c..e69b0d46 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -173,7 +173,27 @@ public: Java: +```Java +class Solution { + int sum; + public TreeNode convertBST(TreeNode root) { + sum = 0; + convertBST1(root); + return root; + } + // 按右中左顺序遍历,累加即可 + public void convertBST1(TreeNode root) { + if (root == null) { + return; + } + convertBST1(root.right); + sum += root.val; + root.val = sum; + convertBST1(root.left); + } +} +``` Python: