Search⌘ K
AI Features

Validate Binary Search Tree

Understand how to validate a binary search tree by ensuring each node's left subtree contains smaller keys and the right subtree contains larger keys. Learn to implement this check effectively using depth-first search methods and analyze binary trees within given constraints.

Statement

Given the root of a binary tree, check whether it is a valid binary search tree (BST).

A binary tree is a valid BST if for every node:

  • The left subtree of a node contains only nodes with keys less than the node’s key.

  • The right subtree of a node contains only nodes with keys greater than the node’s key.

  • Both the left and right subtrees are valid BSTs.

Constraints:

  • 104-10^{4} \leq ...