Medium · Trees

Is BST

Given the root of a binary tree, return true if it is a valid binary search tree (every node's value is strictly greater than all values in its left subtree and strictly less than all values in its right subtree) and false otherwise. An empty tree is valid.

Examples

Example 1

{
  "tree": [5, 3, 8, 2, 4, 7, 9]
}

Output: valid BST

Example 2

{
  "tree": [5, 3, 8, 1, 6]
}

Output: not a BST

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Trees problems