美文网首页
98. 验证二叉搜索树

98. 验证二叉搜索树

作者: 寂灭天骄小童鞋 | 来源:发表于2020-03-16 14:02 被阅读0次

https://leetcode-cn.com/problems/validate-binary-search-tree/

//思路:中序遍历,升序
var preNode : TreeNode?


func isValidBST(_ root: TreeNode?) -> Bool {
    if root == nil {return true}
    
    if !isValidBST(root?.left) {return false}
    
    if (preNode != nil) && preNode!.val >= root!.val {return false}
    preNode = root
    
    if !isValidBST(root?.right) {return false}
    
    return true
}

相关文章

网友评论

      本文标题:98. 验证二叉搜索树

      本文链接:https://www.haomeiwen.com/subject/xclgehtx.html