diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index a44452e2..209c989b 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -173,20 +173,24 @@ public: Java: -```java +```Java class Solution { - private int count = 0; + int sum; public TreeNode convertBST(TreeNode root) { - convert(root); + sum = 0; + convertBST1(root); return root; } - private void convert (TreeNode root) { - if (root == null) return; - convert(root.right); - count += root.val; - root.val = count; - convert(root.left); + // 按右中左顺序遍历,累加即可 + public void convertBST1(TreeNode root) { + if (root == null) { + return; + } + convertBST1(root.right); + sum += root.val; + root.val = sum; + convertBST1(root.left); } } ```