Skip to content
Preptima
mediumCodingEntryMidSenior

Given a binary tree, decide whether it is a valid binary search tree.

5 min readUpdated 2026-08-15

Answer

Short answer

Every node must fall inside an open interval inherited from its ancestors, not merely compare correctly against its own two children. Recurse carrying a low and high bound, tightening one of them at each descent, or walk in-order and require the sequence to be strictly increasing.

Was this answer helpful?

Likely follow-ups

  • Give me a three-node tree that satisfies every parent-child comparison and is not a BST.
  • Your bounds start at the minimum and maximum int. What input breaks that?
  • How would the answer change if equal keys were allowed in the right subtree?
  • Can you do it in O of 1 extra space, given that recursion costs O of h?

Related questions

Related Topics