Given the root of a binary tree, return true if it is a valid binary search tree, otherwise return false.
A valid binary search tree satisfies the following constraints:
Example 1:
Input: root = [2,1,3]
Output: trueExample 2:
Input: root = [1,2,3]
Output: falseExplanation: The root node's value is 1 but its left child's value is 2 which is greater than 1.
Constraints:
1 <= The number of nodes in the tree <= 1000.-1000 <= Node.val <= 1000
You should aim for a solution with O(n) time and O(n) space, where n is the number of nodes in the given tree.
A brute force solution would involve traversing the tree and, for every node, checking its entire left subtree to ensure all nodes are less than the current node, and its entire right subtree to ensure all nodes are greater. This results in an O(n^2) solution. Can you think of a better way? Maybe tracking values during the traversal would help.
We can use the Depth First Search (DFS) algorithm to traverse the tree. At each node, we need to ensure that the tree rooted at that node is a valid Binary Search Tree (BST). One way to do this is by tracking an interval that defines the lower and upper limits for the node's value in that subtree. This interval will be updated as we move down the tree, ensuring each node adheres to the BST property.
We start with the interval [-infinity, infinity] for the root node. As we traverse the tree, when checking the left subtree, we update the maximum value limit because all values in the left subtree must be less than the current node's value. Conversely, when checking the right subtree, we update the minimum value limit because all values in the right subtree must be greater than the current node's value.
# 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:
left_check = staticmethod(lambda val, limit: val < limit)
right_check = staticmethod(lambda val, limit: val > limit)
def isValidBST(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
if (not self.isValid(root.left, root.val, self.left_check) or
not self.isValid(root.right, root.val, self.right_check)):
return False
return self.isValidBST(root.left) and self.isValidBST(root.right)
def isValid(self, root: Optional[TreeNode], limit: int, check) -> bool:
if not root:
return True
if not check(root.val, limit):
return False
return (self.isValid(root.left, limit, check) and
self.isValid(root.right, limit, check))# 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 valid(node, left, right):
if not node:
return True
if not (left < node.val < right):
return False
return valid(node.left, left, node.val) and valid(
node.right, node.val, right
)
return valid(root, float("-inf"), float("inf"))# 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
q = deque([(root, float("-inf"), float("inf"))])
while q:
node, left, right = q.popleft()
if not (left < node.val < right):
return False
if node.left:
q.append((node.left, left, node.val))
if node.right:
q.append((node.right, node.val, right))
return True