添加 0669.修剪二叉搜索树.md Scala版本

This commit is contained in:
ZongqinWang
2022-05-30 10:50:51 +08:00
parent f6456c3b24
commit df2ff8cc2e

View File

@ -453,7 +453,21 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n
}; };
``` ```
## Scala
递归法:
```scala
object Solution {
def trimBST(root: TreeNode, low: Int, high: Int): TreeNode = {
if (root == null) return null
if (root.value < low) return trimBST(root.right, low, high)
if (root.value > high) return trimBST(root.left, low, high)
root.left = trimBST(root.left, low, high)
root.right = trimBST(root.right, low, high)
root
}
}
```
----------------------- -----------------------