98-Validate Binary Search Tree

Posted kingshine007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了98-Validate Binary Search Tree相关的知识,希望对你有一定的参考价值。

题目:验证一个二叉树是否为二叉排序树

def isValidBST(root):
    inorder = inorder(root)
    return inorder == list(sorted(set(inorder)))
def inorder(root):
    if root is None:
        return []
    return inorder(root.left) + [root.val] +inorder(root.right)

注:

采用遍历二叉树的中序遍历,如果结果为排序,则说明该二叉树是二叉排序树

以上是关于98-Validate Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章