# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def dfs(node):
if node is None:
return float('inf'), float('-inf')
lmin, lmax = dfs(node.left)
if lmin is False:
return False, False
rmin, rmax = dfs(node.right)
if rmin is False:
return False, False
if node.val <= lmax or node.val >= rmin:
return False, False
return max(node.val, lmax), min(node.val, rmin)
fmin, fmax = dfs(root)
return True if fmin is not False else False