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

添加 0669.修剪二叉搜索树 Golang版本
This commit is contained in:
X-shuffle
2021-06-19 11:33:02 +08:00
committed by GitHub
parent a72bbac31e
commit 4a33ef6a04

View File

@ -286,7 +286,33 @@ class Solution:
return root
```
Go
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func trimBST(root *TreeNode, low int, high int) *TreeNode {
if root==nil{
return nil
}
if root.Val<low{//如果该节点值小于最小值,则该节点更换为该节点的右节点值,继续遍历
right:=trimBST(root.Right,low,high)
return right
}
if root.Val>high{//如果该节点的值大于最大值,则该节点更换为该节点的左节点值,继续遍历
left:=trimBST(root.Left,low,high)
return left
}
root.Left=trimBST(root.Left,low,high)
root.Right=trimBST(root.Right,low,high)
return root
}
```