23 · Binary Search Trees

#98 Validate Binary Search Tree

Mediumsolve on LeetCode ↗

frequently reported atBloombergMicrosoftAmazonMetaOracleSalesforce

the problem

In plain words

Return true if the tree is a valid BST: every left-subtree value is strictly less than the node, every right-subtree value strictly greater, and both subtrees are BSTs.

in root = [2,1,3]

out true

in root = [5,1,4,null,null,3,6]

out false

4 is in 5's right subtree but 4 < 5.

  • 1 ≤ number of nodes ≤ 10⁴
  • -2³¹ ≤ Node.val ≤ 2³¹ − 1

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #230
    2. #530
    3. #110
      Balanced Binary Tree
      Easyin Tree Recursion