Validate Binary Search Tree - Complete Solution Guide
Validate Binary Search Tree is LeetCode problem 98, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the root of a binary tree, determine if it is a valid binary search tree (BST) . A valid BST is defined as follows: The left subtree of a node contains only nodes with keys strictly less than the node's key. The right subtree of a node contains only nodes with keys strictly greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: Input: root = [2,1,3] Output: true Example 2: Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The
Detailed Explanation
The problem asks us to determine whether a given binary tree is a valid Binary Search Tree (BST). A valid BST has the following properties: 1. For each node, all nodes in its left subtree must have values strictly less than the node's value. 2. For each node, all nodes in its right subtree must have values strictly greater than the node's value. 3. Both the left and right subtrees of a node must also be valid BSTs. The input is the root of the binary tree. The output is a boolean value indicating whether the tree is a valid BST.
Solution Approach
The provided solution employs a recursive Depth-First Search (DFS) approach to traverse the binary tree. The `validate` function takes a node, a lower bound (`low`), and an upper bound (`high`) as input. It recursively checks if the node's value falls within the specified range. If it doesn't, the tree is not a valid BST. If the node is valid, the function then recursively checks the left subtree, updating the upper bound to the node's value, and the right subtree, updating the lower bound to the node's value. The base case for the recursion is when the node is null, in which case we return `true` (an empty tree is a valid BST).
Step-by-Step Algorithm
- Step 1: Initialize the `validate` function with the root node, a lower bound of negative infinity, and an upper bound of positive infinity.
- Step 2: In the `validate` function, check if the current node is null. If it is, return `true`.
- Step 3: Check if the current node's value is within the specified range (strictly greater than the lower bound and strictly less than the upper bound). If it is not, return `false`.
- Step 4: Recursively call the `validate` function for the left subtree, passing the current node's `low` value as the lower bound and the current node's value as the upper bound.
- Step 5: Recursively call the `validate` function for the right subtree, passing the current node's value as the lower bound and the current node's `high` value as the upper bound.
- Step 6: Return `true` if both the left and right subtrees are valid BSTs according to the recursive calls; otherwise return `false`.
Key Insights
- Insight 1: The core idea is to perform a recursive traversal of the tree while maintaining a valid range (lower bound and upper bound) for each node's value. Initially, the range is unbounded (negative infinity to positive infinity).
- Insight 2: For a node to be valid, its value must fall within the current range. If it does, we recursively check its left subtree with the upper bound updated to the node's value, and its right subtree with the lower bound updated to the node's value.
- Insight 3: Using long or double instead of int for low and high values is crucial to handle the edge cases where node values can be equal to the minimum or maximum integer values.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Tree, Depth-First Search, Binary Search Tree, Binary Tree.
Companies
Asked at: Adobe, Amazon, Apple, Arista Networks, Bloomberg, Citadel, IBM, LinkedIn, Meta, Microsoft, Nvidia, Oracle, SIG, Salesforce, ServiceNow, TikTok, Uber, Wix, Yahoo, Yandex, eBay.