Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CollinShoop/c88dc4f54201996664631052e48f5e27 to your computer and use it in GitHub Desktop.
Save CollinShoop/c88dc4f54201996664631052e48f5e27 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 {
if root == nil {
return true
}
var helper func(root *TreeNode, min int, max int) bool
helper = func(root *TreeNode, min int, max int) bool {
return root.Val > min && root.Val < max &&
(root.Left == nil || helper(root.Left, min, root.Val)) &&
(root.Right == nil || helper(root.Right, root.Val, max))
}
return helper(root, math.MinInt64, math.MaxInt64)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment