Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Validate BST

BSTmedium

Implement `isValidBST(root)` returning whether the tree satisfies the BST property (every left descendant is smaller and every right descendant is larger).

Sample tests

Input: isValidBST({"val":5,"left":{"val":3,"left":null,"right":null},"right":{"val":8,"left":null,"right":null}})
Output: true
Input: isValidBST({"val":5,"left":{"val":6,"left":null,"right":null},"right":null})
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Checking only immediate children misses deeper violations.

Learning resources

  • Wikipedia: Binary search tree
Approach & explanation (try first)

Carrying min/max bounds down the recursion validates the full ordering in O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.