Ordered hierarchical storage with monotonic inorder traversal
38 problems·9 Easy·26 Medium·3 Hard
Pattern Study Guide & Cheat Sheet· O(H) where H = height (O(log N) balanced, O(N) skewed) · O(H) stack depth
▼
Exploit the BST invariant (every left descendant < root < every right descendant) to search, insert, and validate in O(height) time without inspecting the whole tree.
Core Invariant: BST Invariant: Validating a node requires verifying it against an ancestor range `(low, high)`, NOT just its immediate parent. Inorder traversal of a BST always yields strictly sorted ascending order.
Recognize it (Keywords & Signals)
Validate Binary Search Tree
Lowest Common Ancestor in BST
Kth smallest / largest element in BST
Convert sorted array / list to balanced BST
Delete / Insert node in BST
When NOT to use
Tree does not follow BST ordering property or duplicates are allowed without clear convention.
How to solve (Step-by-step)
1.For validation: pass valid bounds `(low, high)` down the tree: `low < node.val < high`.
2.For search/LCA: compare `p.val` and `q.val` with `curr.val`. If both smaller, go left; if both larger, go right; if they split, `curr` IS the LCA!
3.For kth smallest: execute iterative inorder traversal using stack; the k-th popped node is the k-th smallest.
Watch for (Interview Traps)
Only checking immediate parent: `node.left.val < node.val` allows invalid grand-ancestors (e.g. right child of left subtree > root)
Handling `<` vs `<=`: LeetCode standard BST has strictly distinct values (`low < val < high`)
Forgetting that BST operations degrade to O(N) if the tree is unbalanced
def is_valid_bst(root: TreeNode | None) -> bool:
def validate(node: TreeNode | None, low: float, high: float) -> bool:
if not node:
return True
# Node value must strictly lie within inherited ancestor bounds
if not (low < node.val < high):
return False
# Left children must be < node.val; Right children must be > node.val
return (validate(node.left, low, node.val) and
validate(node.right, node.val, high))
return validate(root, float('-inf'), float('inf'))
Cost
O(H) where H = height (O(log N) balanced, O(N) skewed) · O(H) stack depth (Inorder traversal yields sorted order in O(N) time.)
Canonical problems
#98 Validate Binary Search Tree:Verify ancestor bounds (low, high) on every node
#235 Lowest Common Ancestor of a BST:O(H) search: split point between p and q is the LCA
#230 Kth Smallest Element in a BST:Inorder traversal visits nodes in sorted order; stop at k
#108 Convert Sorted Array to Binary Search Tree:Pick middle element as root and recurse on halves
Binary Search Tree (BST) frequently asked questions
What is the Binary Search Tree (BST) pattern in coding interviews?
Ordered hierarchical storage with monotonic inorder traversal It is categorized under Trees & Graphs and features 38 curated interview problems across Easy (9), Medium (26), and Hard (3) difficulties.
Which tech companies frequently test Binary Search Tree (BST)?
Companies frequently asking Binary Search Tree (BST) questions include Amazon, Google, Meta, Microsoft, Bloomberg, NeetCode 150.
How do I master the Binary Search Tree (BST) pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.