Validate Binary Search Tree
Medium · Trees
Jump to codeDecide whether every node in a binary tree obeys strict BST ordering: all values in its left subtree are smaller, and all in its right subtree are larger.
Examples
Example 1
root = [2,1,3]
Output: true
Example 2
root = [5,1,4,null,null,3,6]
Output: false
Constraints
- 1 <= The number of nodes in the tree <= 1000
- -1000 <= Node.val <= 1000
Function contract
Trees use level-order arrays, with null for a missing child. Your code receives TreeNode objects with val, left, and right fields.
Need a hint?
Checking only immediate children misses violations inherited from ancestors.
Asked in real interviews
Candidates reported versions of Validate Binary Search Tree from these company interviews. Some company questions are locked and open with Premium or XP.
- Determine Whether a Binary Tree Is a Valid BSTAmazon
- Validate a Binary Search TreePinduoduo
- Validate a Binary Search Tree IterativelyLyft
Solution approach
Traverse with exclusive lower and upper bounds. Reject a value outside its allowed range. For a left child, tighten the upper bound to the current value; for a right child, tighten the lower bound. Null subtrees are valid. Duplicate values fail strict ordering.
Complexity
O(n) time and O(h) stack space.
Python reference solution
def isValidBST(root):
def check(node, low, high):
if node is None:
return True
if not low < node.val < high:
return False
return check(node.left, low, node.val) and check(node.right, node.val, high)
return check(root, float('-inf'), float('inf'))
JavaScript solution
function isValidBST(root) {
function check(node, low, high) {
if (!node) return true;
if (node.val <= low || node.val >= high) return false;
return check(node.left, low, node.val) && check(node.right, node.val, high);
}
return check(root, -Infinity, Infinity);
}
Java solution
import java.util.*;
class Solution {
public boolean isValidBST(TreeNode root) {
return check(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean check(TreeNode node, long low, long high) {
if (node == null) return true;
if (node.val <= low || node.val >= high) return false;
return check(node.left, low, node.val) && check(node.right, node.val, high);
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isValidBST(TreeNode* root) {
return check(root, LLONG_MIN, LLONG_MAX);
}
bool check(TreeNode* node, long long low, long long high) {
if (!node) return true;
if (node->val <= low || node->val >= high) return false;
return check(node->left, low, node->val) && check(node->right, node->val, high);
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 9 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
2,
1,
3
]
]true
[
[
5,
1,
4,
null,
null,
3,
6
]
]false
[ [] ]
true
[
[
1
]
]true
[
[
10,
5,
15,
null,
null,
6,
20
]
]false
Drafts are saved per language in this browser when storage is available.