Search⌘ K
AI Features

Problem: Validate Binary Search Tree

Explore how to validate a binary search tree by applying recursive checks with lower and upper bounds on node values. Understand the importance of verifying tree properties across all ancestors, and learn a step-by-step approach to implement this solution efficiently in Python. Gain insight into the time and space complexity considerations of this method.

Statement

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

A valid BST satisfies the following properties:

  • The left subtree of every node contains only nodes with keys strictly less than that node’s key.

  • The right subtree of every node contains only nodes with keys strictly greater than that node’s key.

  • Both the left and right subtrees must themselves be valid binary search trees.

Return true if the given binary tree is a valid BST, and false otherwise.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4] ...