Dataford
Interview Guides
Upgrade
All questions/Coding/Validate Binary Search Tree

Validate Binary Search Tree

Easy
Coding
Asked at 5 companies5TreesRecursionSearching
Also asked at
FoursquareGoogleKLAMicrosoftAncestry

Problem

Problem

At Stripe, a service stores ordered values in a binary tree and needs to verify that the structure is a valid binary search tree (BST). Write a function that returns True if the tree is valid and False otherwise.

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

  1. All values in its left subtree are strictly less than the node's value.
  2. All values in its right subtree are strictly greater than the node's value.
  3. Both left and right subtrees are also valid BSTs.

Formal Specification

  • Input: root, the root node of a binary tree. Each node has fields val, left, and right.
  • Output: A boolean indicating whether the tree satisfies BST rules.

Examples

Example 1

  • Input: root = [2,1,3]
  • Output: true
  • Explanation: 1 < 2 < 3, and both subtrees satisfy BST rules.

Example 2

  • Input: root = [5,1,4,null,null,3,6]
  • Output: false
  • Explanation: Although 4 is to the right of 5, its left child 3 is also in the right subtree of 5 and must be greater than 5, which it is not.

Constraints

  • The number of nodes is in the range [0, 10^4]
  • -2^31 <= Node.val <= 2^31 - 1
  • Node values must be compared using strict inequality; duplicates make the tree invalid

Examples

Example 1
Inputroot = [2,1,3]OutputtrueWhyEvery node satisfies the BST ordering rules, and both subtrees are valid.
Example 2
Inputroot = [5,1,4,null,null,3,6]OutputfalseWhyNode `3` appears in the right subtree of `5` but is less than `5`, so the tree is not a valid BST.
Example 3
Inputroot = [2,2,3]OutputfalseWhyThe left child equals the root, but BST values must be strictly smaller on the left.

Constraints

  • The number of nodes is in the range [0, 10^4]
  • -2^31 <= Node.val <= 2^31 - 1
  • Each node has fields val, left, and right
  • Duplicates are not allowed in a valid BST

Function Signature

def is_valid_bst(root):

Problem

Problem

At Stripe, a service stores ordered values in a binary tree and needs to verify that the structure is a valid binary search tree (BST). Write a function that returns True if the tree is valid and False otherwise.

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

  1. All values in its left subtree are strictly less than the node's value.
  2. All values in its right subtree are strictly greater than the node's value.
  3. Both left and right subtrees are also valid BSTs.

Formal Specification

  • Input: root, the root node of a binary tree. Each node has fields val, left, and right.
  • Output: A boolean indicating whether the tree satisfies BST rules.

Examples

Example 1

  • Input: root = [2,1,3]
  • Output: true
  • Explanation: 1 < 2 < 3, and both subtrees satisfy BST rules.

Example 2

  • Input: root = [5,1,4,null,null,3,6]
  • Output: false
  • Explanation: Although 4 is to the right of 5, its left child 3 is also in the right subtree of 5 and must be greater than 5, which it is not.

Constraints

  • The number of nodes is in the range [0, 10^4]
  • -2^31 <= Node.val <= 2^31 - 1
  • Node values must be compared using strict inequality; duplicates make the tree invalid

Examples

Example 1
Inputroot = [2,1,3]OutputtrueWhyEvery node satisfies the BST ordering rules, and both subtrees are valid.
Example 2
Inputroot = [5,1,4,null,null,3,6]OutputfalseWhyNode `3` appears in the right subtree of `5` but is less than `5`, so the tree is not a valid BST.
Example 3
Inputroot = [2,2,3]OutputfalseWhyThe left child equals the root, but BST values must be strictly smaller on the left.

Constraints

  • The number of nodes is in the range [0, 10^4]
  • -2^31 <= Node.val <= 2^31 - 1
  • Each node has fields val, left, and right
  • Duplicates are not allowed in a valid BST

Function Signature

def is_valid_bst(root):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
FoursquareValidate Binary Search TreeMediumMetaCheck Binary Tree BalanceEasyMetaCheck Height-Balanced Binary TreeMedium
Next question