Created
January 23, 2022 04:23
-
-
Save CollinShoop/c88dc4f54201996664631052e48f5e27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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