본문 바로가기

Algorithm/LeetCode

[LeetCode][Python3] 98. Validate Binary Search Tree

반응형
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        def isValid(node:TreeNode,low=-math.inf,high=math.inf):
            if not node:
                return True
            else:
                if(node.val<=low or node.val>=high):
                    return False
            return isValid(node.left,low,node.val) and isValid(node.right,node.val,high)
        return isValid(root)

 

반응형