Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created July 2, 2020 07:46
Show Gist options
  • Select an option

  • Save alldroll/eb7eb5886abb5884be63bbca585e3022 to your computer and use it in GitHub Desktop.

Select an option

Save alldroll/eb7eb5886abb5884be63bbca585e3022 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 isValidBST(root *TreeNode) bool {
return checkIsValid(root, -1 << 32, (1 << 32) - 1)
}
func checkIsValid(root *TreeNode, from, to int) bool {
if root == nil {
return true
}
if root.Val <= from || root.Val >= to {
return false
}
return checkIsValid(root.Left, from, root.Val) && checkIsValid(root.Right, root.Val, to)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment