Coding interview problems โบ Trees & BSTs
๐ณ Trees & BSTs Medium
Validate Binary Search Tree
Validate a binary search tree explained: why checking only parent and child fails, the min/max bounds fix, tested Python code, edge cases and a live BST visual.
Also searched as: check if a binary tree is a BST ยท is valid BST with min and max bounds
Try it live Free
Binary Search Tree & Traversals
Insert values in the BST lab and run in-order: a valid BST always prints its values in increasing order, which is the second way to validate one.
The problem
Given the root of a binary tree, decide whether it is a valid binary search tree: for every node, all values in its left subtree are strictly smaller and all values in its right subtree are strictly larger, and both subtrees are BSTs themselves.
3 sits in the right subtree of 5 but is smaller than 5.
The key insight
Comparing each node only with its direct children is the classic trap: a value deep in the right subtree can still be smaller than an ancestor. The fix is to pass down an allowed range (low, high). Going left tightens the upper bound to the current value; going right tightens the lower bound. Every node must fall strictly inside its range.
Step-by-step approach
- Start at the root with the range
(-inf, +inf). - If the node is
None, it is valid. - If
node.valis not strictly betweenlowandhigh, returnFalse. - Recurse left with range
(low, node.val)and right with range(node.val, high). - The tree is valid only if both recursive calls are valid.
Python solution
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val, self.left, self.right = val, left, right
def is_valid_bst(root):
def ok(node, low, high):
if node is None:
return True
if not (low < node.val < high):
return False
return ok(node.left, low, node.val) and ok(node.right, node.val, high)
return ok(root, float("-inf"), float("inf"))Complexity
Each node is checked once against its range. The recursion depth is the tree height h.
Common mistakes
- Only checking
node.left.val < node.val < node.right.val, which accepts trees like[5, 1, 4, null, null, 3, 6]. - Using
<=when the problem forbids duplicates (or the reverse); ask how equal values should be treated. - Initialising bounds with a sentinel such as
0or the minimum 32-bit integer, which breaks when a node holds that exact value; use infinity orNone.
Frequently asked questions
What is the time and space complexity of validating a BST?
O(n) time because each node is checked once, and O(h) space for the recursion stack. The inorder-list approach adds O(n) for the list.
Why is checking parent and child not enough?
The BST rule applies to whole subtrees, not just children. A node can be larger than its parent yet smaller than its grandparent while sitting in the grandparent's right subtree, which breaks the rule.
How do I validate using inorder traversal?
Do an inorder walk while remembering the previous value. If any value is not strictly greater than the previous one, the tree is not a BST. This uses O(h) space if you keep only the previous value.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Trees & BSTs Binary Search Tree & Traversals lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.