Validate Binary Search Tree
Updated
Problem statement
Given the root of a binary tree, return true if it is a valid binary search tree and false otherwise.
Check every node. Every value in its left subtree must be strictly smaller than the node's value. Every value in its right subtree must be strictly greater. Both subtrees must follow the same rules. So duplicate values make the tree invalid.
The tree is given as a list of values, read level by level from left to right. A null marks a missing child.
Function
validateBST(root: TreeNode) → booleanExamples
Example 1
root = [4,2,5,1,3]return = trueNode 2 has a smaller left child, 1, and a larger right child, 3. The whole left subtree is smaller than the root 4. Node 5 on the right is larger than 4. So the answer is true.
Example 2
root = [5,1,7,null,null,4,8]return = falseNode 4 sits in the right subtree of the root 5. Every value on the root's right must be larger than the root, but this value is smaller. So the answer is false.
Constraints
1 <= number of nodes <= 5000-10^6 <= node.val <= 10^6
More InMobi Onsite Interview questions
- Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
- Minimum and Maximum with Optimal ComparisonsONSITE INTERVIEW · Seen Dec 2025
- Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
- Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
- Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
- Word SquaresONSITE INTERVIEW · Seen Jan 2022
- Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
- Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
- Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
- Trapping Rain WaterONSITE INTERVIEW · Seen May 2021
Similar questions at other companies
- Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
- Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
- Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
- Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Serialize a Binary Search TreeConfluent · ONSITE INTERVIEWConfluent interview questions