Recursive
For BST . So, for the left subtree,
is an upper bound. Similarly, for the right subtree,
is a lower bound. We recursively validate the invariant:
.

Time: , space:
.
# 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: Optional[TreeNode]) -> bool:
def is_bst(u, lower_bound, upper_bound):
if not u:
return True
if not (lower_bound < u.val < upper_bound):
return False
return is_bst(u.left, lower_bound, u.val) and is_bst(
u.right, u.val, upper_bound
)
return is_bst(root, float("-inf"), float("inf"))
Iterative
With an explicit stack, we simulate the top-down recursion.
# 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: Optional[TreeNode]) -> bool:
if not root:
return True
stack = [(root, float("-inf"), float("inf"))]
while stack:
u, lower_bound, upper_bound = stack.pop()
if not (lower_bound < u.val < upper_bound):
return False
if u.left:
stack.append( (u.left, lower_bound, u.val) )
if u.right:
stack.append( (u.right, u.val, upper_bound) )
return True
Leave a comment