Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

root =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

A BST is valid if every node value lies within a strict range determined by its ancestors.

Approach

DFS with bounds (lo, hi). For each node, require lo < val < hi. Recurse left with upper bound val, right with lower bound val.

Why it works

The bounds encode all ancestor constraints, not just the parent constraint.

Code
Loading...
Complexity
Time: O(n)
Space: O(h)
Solution
Updated: 2026-02-23

Idea

A BST is valid if every node value lies within a strict range determined by its ancestors.

Approach

DFS with bounds (lo, hi). For each node, require lo < val < hi. Recurse left with upper bound val, right with lower bound val.

Why it works

The bounds encode all ancestor constraints, not just the parent constraint.

Code
Loading...
Complexity
Time: O(n)
Space: O(h)