mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #62 from QuinnDK/添加0098验证二叉搜索树Go版本
添加0098验证二叉搜索树 Go版本
This commit is contained in:
@ -260,7 +260,25 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func isValidBST(root *TreeNode) bool {
|
||||||
|
if root == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return isBST(root, math.MinInt64, math.MaxFloat64)
|
||||||
|
}
|
||||||
|
func isBST(root *TreeNode, min, max int) bool {
|
||||||
|
if root == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if min >= root.Val || max <= root.Val {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user