Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isValidBST(self, root: Optional[TreeNode]) -> bool:
- return self.helper(root, -math.inf, math.inf)
- def helper(self, root, minVal, maxVal):
- if not root:
- return True
- # ensure the root is in the prescribed range
- if not (minVal < root.val < maxVal):
- return False
- # check condition for left subtree
- left = self.helper(root.left, minVal, root.val)
- # check condition for right subtree
- right = self.helper(root.right, root.val, maxVal)
- # return if both the subtree condition are satisfied
- return left and right
Advertisement
Add Comment
Please, Sign In to add comment