Validate Binary Search Tree

Question

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

A valid BST is defined as follows:

  • 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 must also be binary search trees.

Input: root = [2,1,3]

Output: true

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

Output: false

The root node's value is 5 but its right child's value is 4.

Clarify the problem

What are some questions you'd ask an interviewer?

main.py

Login or signup to save your code.

Bookmark
Reset

Looks like you don't have access yet

Members get the full walkthrough, the working code, and the complexity explained.

Get full access

Not sure what this unlocks? Check out a free pattern section.

Slides Solution

Code Solution

Example Solution

Complexity

Time Complexity and Space Complexity