#98

Validate Binary Search Tree

Medium Bounds passed down

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether a binary tree is a valid BST: every node is strictly greater than all nodes in its left subtree and strictly less than all in its right subtree.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: is every node within the range its ancestors allow?

Why this works: pass (low, high) down: the left child gets (low, node), the right child gets (node, high).

Example: 5 (1, 4 (3, 6))

4 must be > 5 (right of 5) → fails → false

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Comparing only with the direct parent — a deep right descendant of a left child can still be too large.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h).
Topic: Binary Search Trees
🎬 Episodes 61–63: video coming soon