<reponame>graveszhang/LeetCode-Practice
# 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

        # if root.left and root.right:
        #     if v <= root.left.val or v >= root.right.val: return False
        # elif root.left: 
        #     if v <= root.left.val: return False
        # elif root.right:
        #     if v >= root.right.val: return False
        
class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        self.prev = None    
        return self.recur(root)

    def recur(self, root):
        if not root: return True
        left = self.recur(root.left)
        if self.prev and self.prev.val >= root.val: 
            return False
        self.prev = root
        right = self.recur(root.right)
        return left and right

# 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

        # if root.left and root.right:
        #     if v <= root.left.val or v >= root.right.val: return False
        # elif root.left: 
        #     if v <= root.left.val: return False
        # elif root.right:
        #     if v >= root.right.val: return False
        
class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        self.prev = None    
        return self.recur(root)

    def recur(self, root):
        if not root: return True
        left = self.recur(root.left)
        if self.prev and self.prev.val >= root.val: 
            return False
        self.prev = root
        right = self.recur(root.right)
        return left and right
# 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 # if root.left and root.right: #     if v <= root.left.val or v >= root.right.val: return False # elif root.left: #     if v <= root.left.val: return False # elif root.right: #     if v >= root.right.val: return False