Determine if a given binary tree adheres to the rules of a Binary Search Tree (BST). This problem tests your understanding of tree traversals and BST properties, critical for many tree-based algorithms.
You are given the root of a binary tree. Write a function to determine if the tree is a valid Binary Search Tree (BST). A valid BST is defined as follows: for each node in the tree, all nodes in its left subtree must have values strictly less than the node's value, and all nodes in its right subtree must have values strictly greater than the node's value. This property must hold for all nodes in the tree.
The brute-force approach would involve checking every possible path in the tree to see if it violates the BST property. It's like manually checking every shelf in a library to ensure books are in the correct order, one by one. This is extremely inefficient, requiring you to compare each node with every other node in the tree.
Work through this problem with AI coaching and get real-time feedback
Practice This Problem