mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加 0669.修剪二叉搜索树 Golang版本
添加 0669.修剪二叉搜索树 Golang版本
This commit is contained in:
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user