添加 0538.把二叉搜索树转换为累加树.md Scala版本
This commit is contained in:
parent
474ae02289
commit
ca18b7d1f5
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue