Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Optional

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 validate(node: Optional[TreeNode], min_val: float, max_val: float) -> bool:
# Empty tree is valid
if not node:
return True

# Check if current node violates BST property
if node.val <= min_val or node.val >= max_val:
return False

# Recursively validate left and right subtrees
# Left subtree: all values must be < node.val
# Right subtree: all values must be > node.val
return (validate(node.left, min_val, node.val) and
validate(node.right, node.val, max_val))

return validate(root, float('-inf'), float('inf'))
Loading