Validate Binary Search Tree
Updated
Problem statement
A binary tree is stored in three arrays of the same length. Node i holds the value values[i]. left[i] is the index of its left child, and right[i] is the index of its right child. A child index is -1 when that child is missing. root is the index of the root node.
Return true if the tree is a valid binary search tree, and false otherwise.
In a valid binary search tree, every value in a node's left subtree is strictly smaller than the node's value. Every value in its right subtree is strictly greater. So duplicate values make the tree invalid.
Function
isValidBST(values: int[], left: int[], right: int[], root: int) → booleanExamples
Example 1
values = [2,1,3]left = [1,-1,-1]right = [2,-1,-1]root = 0return = trueThe left child 1 is smaller than the root 2, and the right child 3 is greater. The tree is valid, so the answer is true.
Example 2
values = [5,1,4,3,6]left = [1,-1,3,-1,-1]right = [2,-1,4,-1,-1]root = 0return = falseThe value 4 sits in the right subtree of the root 5, but it is not greater than 5. So the answer is false.
Constraints
0 <= values.length <= 1000.left.length = right.length = values.length.- Node values are unique signed 32-bit integers.
- Child entries are
-1or valid node indices, and the arrays describe one finite binary tree. root = -1exactly when the tree is empty; otherwise it is a valid node index.
More Lyft interview questions
- Minimum Window SubstringPHONE SCREEN · Seen Oct 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · Seen Oct 2026
- Stateful Paginated Fetch NPHONE SCREEN · Seen Sep 2026
- Reconstruct a BST from PreorderPHONE SCREEN · Seen Aug 2026
- Keyboard Autocomplete SuggestionsONSITE INTERVIEW · Seen Nov 2026
- Nested Transaction Key-Value Store with Value CountsONSITE INTERVIEW · Seen Sep 2026
- Globally Versioned Key-Value StoreONSITE INTERVIEW · Seen Sep 2026
- Rotten Oranges / Grid Infection BFSOA · Seen Aug 2026
- Minimum Workers with Job AssignmentsONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Count Uni-Valued SubtreesAmazon · PHONE SCREEN
- Lowest Common Ancestor in a Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Check Whether a Tree Is a Sum TreeAdobe · PHONE SCREENAdobe interview questions
- Lowest Common Ancestor in a Binary TreeZillow · PHONE SCREENZillow interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Trim a Binary Search TreeBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions