Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created February 5, 2020 15:28
Show Gist options
  • Save alldroll/8e5b55087e92479da71794e6dca667a5 to your computer and use it in GitHub Desktop.
Save alldroll/8e5b55087e92479da71794e6dca667a5 to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/validate-binary-search-tree
/**
* 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
}
if root.Left != nil && root.Val <= max(root.Left) {
return false
}
if root.Right != nil && root.Val >= min(root.Right) {
return false
}
return isValidBST(root.Left) && isValidBST(root.Right)
}
func min(root *TreeNode) int {
if root.Left == nil {
return root.Val
}
return min(root.Left)
}
func max(root *TreeNode) int {
if root.Right == nil {
return root.Val
}
return max(root.Right)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment