Validate a Binary Search Tree

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write isValidBST(root). A binary tree is made of nodes { value, left, right } holding numbers, where left and right are child nodes or null; an empty tree is null. Return true if it's a binary search tree: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. Equal values aren't allowed, and an empty tree is valid. For example, the tree written in level order as [5, 4, 6, null, null, 3, 7] is not valid: 3 is rightly the left child of 6, but it's in the right subtree of 5 and smaller than 5.

Your code must pass

  • Accepts a valid search tree
  • Rejects a child on the wrong side
  • Rejects a deeper node that breaks an ancestor's limit
  • Rejects equal values
  • Accepts an empty tree and a single node