添加 0538.把二叉搜索树转换为累加树.md Scala版本

This commit is contained in:
ZongqinWang
2022-05-31 22:13:58 +08:00
parent 474ae02289
commit ca18b7d1f5

View File

@ -352,6 +352,24 @@ function convertBST(root: TreeNode | null): TreeNode | null {
}; };
``` ```
## Scala
```scala
object Solution {
def convertBST(root: TreeNode): TreeNode = {
var sum = 0
def convert(node: TreeNode): Unit = {
if (node == null) return
convert(node.right)
sum += node.value
node.value = sum
convert(node.left)
}
convert(root)
root
}
}
```
----------------------- -----------------------