Created
February 5, 2020 15:28
-
-
Save alldroll/8e5b55087e92479da71794e6dca667a5 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
// 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