Skip to content

Instantly share code, notes, and snippets.

@martende
Created June 25, 2021 16:34
Show Gist options
  • Save martende/f846225c1d4e7428128e3bb052bd7cac to your computer and use it in GitHub Desktop.
Save martende/f846225c1d4e7428128e3bb052bd7cac to your computer and use it in GitHub Desktop.
/**
* 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 && root.Val <= high {
return &TreeNode{
Val : root.Val,
Left: trimBST(root.Left,low,high),
Right: trimBST(root.Right,low,high),
}
}
a := trimBST(root.Left,low,high)
if a != nil {
return a
}
return trimBST(root.Right,low,high)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment