Data Structures19 sections · 729 units
Open in Course

Problem - Validate BST

Is this tree a valid BST?

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

A valid BST satisfies:

  • Left subtree contains only nodes with values less than the node's value
  • Right subtree contains only nodes with values greater than the node's value
  • Both subtrees are also valid BSTs

Example: [2,1,3][2,1,3] is valid.

[5,1,4,null,null,3,6][5,1,4,null,null,3,6] is not (4 is in the right subtree of 5 but 3 < 5). Constraints: up to 10410^4 nodes, values are 32-bit integers.