Problem
Section titled “Problem”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:
![Tree [2,1,3]](https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg)
Input: root = [2,1,3]Output: trueExample 2:
![Tree [5,1,4,null,null,3,6]](https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg)
Input: root = [5,1,4,null,null,3,6]Output: falseExplanation: The root node's value is 5 but its right child's value is 4.Constraints:
- The number of nodes in the tree is in the range .
-
Node.val
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Comparing each node only with its own children is not enough. In the tree below every parent–child pair looks fine, yet 3 sits in the right subtree of 5 and is smaller than 5:
The fix is to pass down the constraints of all ancestors. Each node gets an open interval (lo, hi) its value must lie in. The root may be anything. Going left from a node with value v keeps lo and sets hi = v (everything there must be smaller than v, and still satisfy the older bounds); going right sets lo = v. Each interval is the intersection of all the ancestor constraints on that path, so a tree is a BST exactly when every node lies inside its interval. In the picture, 3 must lie in (5, 6) and fails.
Node values span the full int range, so the “no bound yet” values must lie outside it; the bounds are long long starting at LLONG_MIN / LLONG_MAX.
Algorithm
Section titled “Algorithm”- Call
valid(root, LLONG_MIN, LLONG_MAX). - In
valid(node, lo, hi): a null node is valid, so returntrue. - If
node->val <= loornode->val >= hi, returnfalse(the bounds are strict, so duplicates are rejected). - Return
valid(node->left, lo, node->val) && valid(node->right, node->val, hi);&&stops as soon as one side fails.
Complexity
Section titled “Complexity”- Time: — each node is checked once.
- Space: — the recursion stack, where is the tree height ( for a skewed tree, at most frames here).
Edge cases
Section titled “Edge cases”- Duplicate values (
[2,2]): the child’s interval is(-∞, 2)and2 <= 2fails, as the strict definition requires. - Values at
INT_MIN/INT_MAX([2147483647]): they lie strictly inside(LLONG_MIN, LLONG_MAX), so a single extreme node is valid, and noval ± 1arithmetic can overflow.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { // Every value in this subtree must lie strictly between lo and hi. bool valid(TreeNode* node, long long lo, long long hi) { if (!node) return true; if (node->val <= lo || node->val >= hi) return false; return valid(node->left, lo, node->val) && valid(node->right, node->val, hi); }
public: bool isValidBST(TreeNode* root) { return valid(root, LLONG_MIN, LLONG_MAX); }};class Solution { // Every value in this subtree must lie strictly between lo and hi. private boolean valid(TreeNode node, long lo, long hi) { if (node == null) return true; if (node.val <= lo || node.val >= hi) return false; return valid(node.left, lo, node.val) && valid(node.right, node.val, hi); }
public boolean isValidBST(TreeNode root) { return valid(root, Long.MIN_VALUE, Long.MAX_VALUE); }}class Solution: def isValidBST(self, root: TreeNode | None) -> bool: # Every value in a node's subtree must lie strictly between its lo and hi. stack = [(root, float('-inf'), float('inf'))] while stack: node, lo, hi = stack.pop() if not node: continue if node.val <= lo or node.val >= hi: return False stack.append((node.right, node.val, hi)) stack.append((node.left, lo, node.val)) return TrueGive it a try first
Attempt the problem on LeetCode — the code will still be here.
