medium

Validate Binary Search Tree

Given the root of a binary tree, determine whether it is a valid binary search tree: every node in a left subtree is strictly less than its ancestor, every node in a right subtree strictly greater, and all subtrees are themselves valid.

Constraints
  • 1 ≤ number of nodes ≤ 10^4
  • -2^31 ≤ node value ≤ 2^31 - 1
Examples
in: root = [2,1,3]
out: true
in: root = [5,1,4,null,null,3,6]
out: false
Node 3 sits in the right subtree of 5.

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.