Algorithms Playbook
← All problems
MediumBinary search trees →Similar to: Validate Binary Search Tree

Is it really a search tree?

Problem

Given the root of a binary tree, return whether it is a valid binary search tree: for every node, all values in its left subtree are strictly smaller and all values in its right subtree are strictly bigger.

Examples

Input: [2, 1, 3]

Output: true

Input: [5, 1, 4, null, null, 3, 6]

Output: false

3 is in the right subtree of 5 but is smaller than 5.

Hints

    Try it first. Every hint reveals a bit more.

    Solution