Validate Binary Search Tree

Medium

Description

Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as: The left subtree contains only nodes with keys less than the node's key. The right subtree contains only nodes with keys greater than the node's key. Both subtrees must also be valid BSTs.

Examples

Input:root = [2,1,3]
Output:true
Explanation:

Valid BST: 1 < 2 < 3

Input:root = [5,1,4,null,null,3,6]
Output:false
Explanation:

The root's right child is 4 which is less than 5.

Input:root = [1]
Output:true
Explanation:

The target value exists in the given data structure.

Constraints

  • The number of nodes in the tree is in the range [1, 10⁴]
  • -2³¹ ≤ Node.val ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!