mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0538.把二叉搜索树转换为累加树.md
添加 0538.把二叉搜索树转换为累加树 Java版本
This commit is contained in:
@ -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:
|
||||
|
||||
|
Reference in New Issue
Block a user