1 Star 0 Fork 1

dllearn/geektime-algorithm-learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
f.go 656 Bytes
一键复制 编辑 原始数据 按行查看 历史
dryyun 提交于 2022-04-25 17:17 +08:00 . 0098
package validatebinarysearchtree
import . "gitee.com/dllearn/geektime-algorithm-learn/structures"
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidBST(root *TreeNode) bool {
const MaxUint = ^uint(0)
// const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
return check(root, MinInt, MaxInt)
}
func check(node *TreeNode, left, right int) bool {
if node == nil {
return true
}
if node.Val < left || node.Val > right {
return false
}
return check(node.Left, left, node.Val-1) && check(node.Right, node.Val+1, right)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dllearn/geektime-algorithm-learn.git
git@gitee.com:dllearn/geektime-algorithm-learn.git
dllearn
geektime-algorithm-learn
geektime-algorithm-learn
55fa3f222b36

搜索帮助