98.验证二叉搜索树

leetcode

递归实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func isValidBST(root *TreeNode) bool {
return dfs(root, math.MinInt64, math.MaxInt64)
}

func dfs(root *TreeNode, min int, max int) bool {
if root == nil {
return true
}

// 注意和上下边界相等的情况应该返回false
if root.Val >= max || root.Val <= min {
return false
}

return dfs(root.Left, min, root.Val) && dfs(root.Right, root.Val, max)
}

98.验证二叉搜索树
https://blog.jerrylee.me/2021/09/f12d6c038a19.html
作者
Jerry Lee
发布于
2021年9月20日
许可协议