mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
33 lines
596 B
Go
33 lines
596 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* 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 root
|
|
}
|
|
if root.Val > high {
|
|
return trimBST(root.Left, low, high)
|
|
}
|
|
if root.Val < low {
|
|
return trimBST(root.Right, low, high)
|
|
}
|
|
root.Left = trimBST(root.Left, low, high)
|
|
root.Right = trimBST(root.Right, low, high)
|
|
return root
|
|
}
|