LeetCode ยท #98

Validate Binary Search Tree Solution

Given the root of a binary tree, determine if it is a valid BST.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #98

๐Ÿ—๏ธ

Pattern

BST โ€” min/max bounds propagation

Given the root of a binary tree, determine if it is a valid BST. A valid BST has: (1) left subtree values strictly less than the node, (2) right subtree values strictly greater, (3) both subtrees are also valid BSTs.

Example
Input: 5 / \ 1 4 / \ 3 6 Output: false // Node 4 is in the right subtree of 5 but 4 < 5 โ€” violation
Constraints
โ€ข 1 โ‰ค number of nodes โ‰ค 10โด โ€ข -2ยณยน โ‰ค Node.val โ‰ค 2ยณยน - 1
02
Section Two ยท Approach 1

Inorder Traversal โ€” O(n)

A valid BST's inorder traversal produces a strictly increasing sequence. Traverse inorder and verify each value is greater than the previous.

Alternative approach:
  • Works well but requires storing the previous value.
  • The min/max bounds approach is more elegant and doesn't need extra state.
03
Section Three ยท Approach 2

Min/Max Bounds โ€” O(n)

Pass valid value bounds down the tree. Each node must fall within (min, max). When going left, update max to current value. When going right, update min to current value.

๐Ÿ’ก Mental model: Each node has a "permit range" โ€” it can only hold values within that range. The root's range is (-โˆž, +โˆž). Going left narrows the upper bound; going right narrows the lower bound. If any node violates its permit โ†’ invalid BST.
Algorithm
  • Base: null โ†’ true (empty subtree is valid).
  • Check: If node.val โ‰ค min or node.val โ‰ฅ max โ†’ false.
  • Recurse left: validate(left, min, node.val).
  • Recurse right: validate(right, node.val, max).
๐ŸŽฏ Common trap:
  • Only checking left.val < root.val misses deep violations.
  • Node 3 in the example is left of 4 (3 < 4 โœ“) but it's in the right subtree of 5 (3 < 5 โœ—).
  • Bounds propagation catches this because node 3's min bound is 5.
04
Section Four ยท Trace

Visual Walkthrough

Bounds propagation โ€” [5, 1, 4, null, null, 3, 6]
TREE STRUCTURE 5 (-โˆž, +โˆž) 1 (-โˆž, 5) โœ“ 4 (5, +โˆž) โœ— 4 โ‰ค 5 โ†’ INVALID! 3 6 BOUNDS TRACE โ‘  Node 5: range = (-โˆž, +โˆž). 5 is within โ†’ โœ“ valid. Go left with (-โˆž, 5), right with (5, +โˆž). โ‘ก Node 1: range = (-โˆž, 5). 1 < 5 โ†’ โœ“ valid. No children โ†’ return true. โ‘ข Node 4: range = (5, +โˆž). 4 โ‰ค 5 โ†’ โœ— INVALID! Return false immediately.
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” Min/Max Bounds
class Solution { public boolean isValidBST(TreeNode root) { return validate(root, Long.MIN_VALUE, Long.MAX_VALUE); } private boolean validate(TreeNode node, long min, long max) { if (node == null) return true; if (node.val <= min || node.val >= max) return false; return validate(node.left, min, node.val) && validate(node.right, node.val, max); } }
Python โ€” Min/Max Bounds
class Solution: def isValidBST(self, root) -> bool: def validate(node, lo, hi): if not node: return True if node.val <= lo or node.val >= hi: return False return validate(node.left, lo, node.val) \ and validate(node.right, node.val, hi) return validate(root, float('-inf'), float('inf'))
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpace
Min/Max BoundsO(n)O(h)
Inorder + check sortedO(n)O(h)
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Single node[1]Always valid.
Equal values[2,2]BST requires strict < / >. Equal values are invalid.
INT_MIN/MAX values[2147483647]Use long for bounds to avoid overflow when comparing to Integer.MAX_VALUE.
โš  Common Mistake: Only checking direct parent-child relationship: left.val < root.val. This misses cases where a deep-left node in a right subtree violates the BST property with respect to an ancestor. Bounds propagation catches all violations.

โ† Back to BST problems