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:
  1. We check if the node's value lies within the current valid range.
  2. 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.
  3. If any node violates the BST property (i.e., lies outside the valid range), the function will return False immediately.
  4. 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.
  5. Below is a very detailed step-by-step pseudocode on how to implement this:

    Step-by-Step Explanation

  6. Define the main function
    isValidBST
    that accepts the root of the tree.
  7. Define the helper function
    helper
    , which will be called recursively.
  8. The
    helper
    function checks if a given node's value is within a specified range (
    lower
    ,
    upper
    ).
  9. If the node is None (i.e., it's a leaf's child), return True because leaf nodes don't violate BST properties.
  10. 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.
  11. Recursively call the helper function for the right subtree with updated lower bound and for the left subtree with an updated upper bound.
  12. If all subtrees are valid, return True.
  13. 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)
    
                                            

    Explanation of Detailed Steps

  14. Main Function Call (
    isValidBST
    )
    : We start by calling the
    isValidBST
    function with the root node of the binary tree.
  15. Helper Function Definition (
    helper
    )
    : Inside
    isValidBST
    , we define a nested
    helper
    function. The
    helper
    function is responsible for verifying the BST properties recursively for each subtree.
  16. Base Case Check (
    if node == None
    )
    : If we reach a
    None
    node (indicating that we've traversed past a leaf node), the subtree is considered valid, and we return True.
  17. Range Validation (
    if node.val <= lower or node.val >= upper
    )
    : We check if the current node’s value falls within the permissible bounds. If it doesn't, we return False immediately as it violates the BST condition.
  18. Right Subtree Validation : The right subtree’s nodes should be greater than the current node's value. Therefore, we call
    helper
    on the right child with the node’s value as the new lower bound and the original upper bound.
  19. Left Subtree Validation : The left subtree’s nodes should be less than the current node's value. Hence, we call
    helper
    on the left child with the node’s value as the new upper bound and the original lower bound.
  20. Final Validation Check : If all recursive calls return True, the entire tree is considered a BST, and we return True from the
    isValidBST
    function.
By following these steps and the pseudocode provided, we can ensure that our solution correctly determines whether a given binary tree is a valid BST. This method ensures that we have an efficient and reliable way to check the BST properties across all nodes and subtrees of the binary tree.