问题
这是我写的方法,一个元素[0]输入这种情况下,我本地跑出来是 BST,但是 Leetcode submit 结果确实不是
package main
import (
"fmt"
)
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
var preData = MinInt
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isValidBST(root *TreeNode) bool {
if root == nil {
return true
}
isValid := isValidBST(root.Left)
if !isValid || preData >= root.Val {
return false
}
preData = root.Val
isValid = isValidBST(root.Right)
return isValid
}
func main() {
root := &TreeNode{
Val: 0,
Left: nil,
Right: nil,
}
res := isValidBST(root)
if res {
fmt.Printf("Tree is valid BST \n")
} else {
fmt.Printf("Tree is not valid BST \n")
}
}
是我写的有问题吗?