Validate Binary Search Tree

Medium

Topics
TreeDFSBST

Given the root of a binary tree, return true if it is a valid binary search tree: every node in the left subtree is strictly less, every node in the right subtree is strictly greater, and both subtrees are themselves valid BSTs.

Example 1

123
Every node stays inside the open interval its ancestors impose, so the answer is true. Passing a (low, high) bound down the recursion is the fix for the parent-only check; an in-order walk being strictly increasing is the equivalent test.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [2,1,3]
Output: true

Example 2

15346
violates its inherited bound
The shaded node sits inside the right range relative to its parent but outside the range inherited from an ancestor, so the answer is false. This is exactly why comparing each node with only its parent is wrong.
Input:  root = [5,1,4,null,null,3,6]
Output: false

Constraints

  • 1 <= number of nodes <= 10^4
  • -2^31 <= Node.val <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

A node being larger than its left child is not enough — it must be larger than everything in the left subtree. Track an allowed (low, high) open interval that tightens as you descend.

Approach

  1. Recurse with bounds (low, high), initially (-inf, +inf).
  2. Each node must satisfy low < node.val < high.
  3. Recurse left with high = node.val and right with low = node.val.

Dry Run

tree 5 1 4 null null 3 6
      5
     / \
    1   4
       / \
      3   6
Each node inherits an open interval it must lie inside:
  node 5   must be in (-inf, inf)   ok
    node 1   must be in (-inf, 5)   ok
    node 4   must be in (5, inf)    VIOLATION -- 4 is not greater than 5
answer false
This tree is caught by the root's own child, so a lazier check would also catch
it. The interval really earns its place one level deeper. On 5 4 6 null null 3 7
every parent-child pair is individually fine -- 4 < 5, 6 > 5, 3 < 6, 7 > 6 -- but
the 3 sits in the root's RIGHT subtree and must exceed 5, which it does not. A
check that compares only against the immediate parent returns true there; the
interval version returns false.
That is what lo and hi carry: not "bigger than my parent" but "inside the range
every ancestor has already narrowed". Going left replaces hi with the node's
value, going right replaces lo, and neither bound is ever widened again.
The comparison is strict on both sides, which encodes that a BST here holds no
duplicates. Using <= on either end would admit an equal value.

Complexity

  • Time — O(n): each node checked once.
  • Space — O(h): recursion stack.

Common Pitfalls

  • Comparing only against the immediate parent misses violations deeper in a subtree — pass down the full interval. The page's own failing example does not show this, because its violation is a direct child of the root. 5 4 6 null null 3 7 does: every parent-child pair is individually valid, but the 3 lies in the root's right subtree and must exceed 5. A parent-only check returns true there; the interval check returns false.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isValidBST(self, root):
        def valid(node,lo,hi):
            if not node: return True
            if not (lo<node.val<hi): return False
            return valid(node.left,lo,node.val) and valid(node.right,node.val,hi)
        return valid(root,float('-inf'),float('inf'))
Asked at Lyft Ola
SensAI
Beta
Tap the mic to stop voice mode