Validate Binary Search Tree
To solve this coding challenge, we need to determine if a given binary tree is a valid Binary Search Tree (BST). A binary tree is a BST if, for each node in the tree, all the values in the left subtree are less than the node's value, and all the values in the right subtree are greater than the node's value. Additionally, both left and right subtrees must themselves be BSTs.
Explanation
To validate if a binary tree is a BST, we can use a recursive approach. We will use a helper function that checks whether the tree rooted at a given node is a BST within a particular range of values. Initially, the range is between negative infinity and positive infinity. As we traverse the tree:- We check if the node's value lies within the current valid range.
- For the left subtree of the node, we update the upper limit to the node's value, and for the right subtree, we update the lower limit to the node's value.
- If any node violates the BST property (i.e., lies outside the valid range), the function will return False immediately.
- If we reach a leaf node (i.e., a node with no children), we return True, indicating that this part of the tree is a valid BST. Below is a very detailed step-by-step pseudocode on how to implement this:
-
Define the main function
isValidBST
-
Define the helper function
helper
-
The
helper
lower
upper
- If the node is None (i.e., it's a leaf's child), return True because leaf nodes don't violate BST properties.
- If the node's value is less than or equal to the lower bound or greater than or equal to the upper bound, return False.
- Recursively call the helper function for the right subtree with updated lower bound and for the left subtree with an updated upper bound.
- If all subtrees are valid, return True.
-
Main Function Call (
isValidBST
isValidBST
-
Helper Function Definition (
helper
isValidBST
helper
helper
-
Base Case Check (
if node == None
None
-
Range Validation (
if node.val <= lower or node.val >= upper
-
Right Subtree Validation
: The right subtreeβs nodes should be greater than the current node's value. Therefore, we call
helper
-
Left Subtree Validation
: The left subtreeβs nodes should be less than the current node's value. Hence, we call
helper
-
Final Validation Check
: If all recursive calls return True, the entire tree is considered a BST, and we return True from the
isValidBST
Step-by-Step Explanation
Detailed Steps in Pseudocode
# Main function to validate if the binary tree is a BST
function isValidBST(root):
# Helper function definition with initial range set to (-infinity, +infinity)
function helper(node, lower, upper):
# Base case: If the current node is None, return True (it is a valid subtree)
if node == None:
return True
# Check if the current node's value is within the range (lower, upper)
if node.val <= lower or node.val >= upper:
return False
# Recursively validate the right subtree: range (node.val, upper)
if not helper(node.right, node.val, upper):
return False
# Recursively validate the left subtree: range (lower, node.val)
if not helper(node.left, lower, node.val):
return False
# If both subtrees are valid, return True
return True
# Initial call to the helper function with the root and the full range
return helper(root, -infinity, infinity)