Validate Binary Search Tree
Updated
Problem statement
You are given the root of a binary tree. Return true if it is a valid binary search tree (BST), and false otherwise.
A binary tree is a valid BST when every node follows both rules:
- Every value in the node's left subtree is strictly smaller than the node's value.
- Every value in the node's right subtree is strictly larger than the node's value.
The rules apply to the whole subtree, not only to the direct children. Because the comparisons are strict, a tree with two equal values is never a valid BST.
Function
isValidBST(root: TreeNode) → booleanExamples
Example 1
root = [5,1,4,null,null,3,6]return = falseThe root is 5. Its right child is 4, but every value in the right subtree must be larger than 5. So the tree is not a valid BST.
Example 2
root = [2,1,3]return = trueThe left child 1 is smaller than 2, and the right child 3 is larger. Both children are leaves, so the tree is a valid BST.
Example 3
root = [5,4,6,null,null,3,7]return = falseEach node is correct compared with its own children: 4 < 5 < 6 and 3 < 6 < 7. But 3 is in the right subtree of the root 5, and 3 is smaller than 5. So the tree is not a valid BST.
Constraints
- The number of nodes in the tree is in the range
[1, 2000]. -2^31 <= Node.val <= 2^31 - 1
More Kotak Mahindra Bank interview questions
- Longest Common PrefixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
- Palindrome NumberPHONE SCREEN · Seen Nov 2026
- Roman to IntegerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Remove Duplicates From Sorted Array In PlacePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Nov 2026
- Maximum Non-Adjacent Coins in Two RowsONSITE INTERVIEW · Seen Oct 2026
- Search in Rotated Sorted ArrayONSITE INTERVIEW · Seen Oct 2026
- Find the Index of the First Occurrence in a StringONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions