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