mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Merge pull request #1423 from wzqwtt/tree18
添加(0538.把二叉搜索树转换为累加树) 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