← Practice · Trees

Validate Binary Search Tree Medium

Given the root of a binary tree (TreeNode with val, left, right), return true if it is a valid binary search tree: every node in a left subtree is strictly less than its ancestor, and every node in a right subtree is strictly greater, at every level.

Examples

  • [2,1,3] -> true
  • [5,1,4,null,null,3,6] -> false (3 sits in the right subtree of 5 but is smaller)

Define a function named isValidBST in JavaScript. TreeNode (val, left, right) is available.

00:00

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests