判斷一棵樹是不是二叉排序樹

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def dfs(self,root,lower,higher):
        if root==None:
            return True
        if root.val<=lower:
            return False
        if root.val>=higher:
            return False
        return self.dfs(root.left,lower,root.val) and self.dfs(root.right,root.val,higher)
        
    def isValidBST(self, root: TreeNode) -> bool:
        return self.dfs(root,-float('inf'),float('inf'))
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章