码迷,mamicode.com
首页 > 其他好文 > 详细

98-Validate Binary Search Tree

时间:2019-08-18 17:49:52      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:div   roo   sorted   arc   class   order   binary   def   arch   

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

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

标签:div   roo   sorted   arc   class   order   binary   def   arch   

原文地址:https://www.cnblogs.com/kingshine007/p/11372974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!