Binary Search Tree (BST)

Trees & Graphs

Ordered hierarchical storage with monotonic inorder traversal

37 problems·9 Easy·25 Medium·3 Hard
Pattern Study Guide & Cheat Sheet

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. 1.For validation: pass valid bounds `(low, high)` down the tree: `low < node.val < high`.
  2. 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. 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
Validate BST (Range Invariant) & Inorder Traversal
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
Medium·23 companies·Max freq 88%·Acc 36.0%
WixNeetCode 150NeetCode 150+20
Medium·14 companies·Max freq 100%·Acc 70.9%
XXNeetCode 150+11
Medium·14 companies·Max freq 88%·Acc 77.0%
UberNeetCode 150NeetCode 150+11
Medium·11 companies·Max freq 88%·Acc 63.9%
SnapSnapClari+8
Medium·11 companies·Max freq 53%·Acc 55.0%
OracleUberByteDance+8
Medium·10 companies·Max freq 100%·Acc 66.9%
ZenefitsLyftSalesforce+7
Easy·9 companies·Max freq 75%·Acc 75.8%
AirbnbAccentureAmazon+6
Easy·8 companies·Max freq 88%·Acc 49.2%
SnapSnapMeta+5
Easy·8 companies·Max freq 88%·Acc 63.5%
SamsungMetaCisco+5
Medium·8 companies·Max freq 63%·Acc 76.7%
MetaLinkedInMicrosoft+5
Medium·7 companies·Max freq 63%·Acc 73.5%
AndurilMicrosoftAmazon+4
Medium·6 companies·Max freq 100%·Acc 51.2%
Pocket GemsDocusignMicrosoft+3
Easy·6 companies·Max freq 78%·Acc 87.6%
MetaYandexGoogle+3
Medium·6 companies·Max freq 38%·Acc 84.4%
AmazonSalesforceMeta+3
Easy·6 companies·Max freq 25%·Acc 82.9%
GoogleMetaAmazon+3
Medium·5 companies·Max freq 100%·Acc 51.8%
ZenefitsExpediaSalesforce+2
Medium·5 companies·Max freq 38%·Acc 86.3%
AmazonMetaMicrosoft+2
Hard·5 companies·Max freq 38%·Acc 48.1%
AmazonGoogleMicrosoft+2
Medium·5 companies·Max freq 28%·Acc 62.8%
MetaAmazonGoogle+2
Easy·4 companies·Max freq 88%·Acc 61.6%
WixGoogleMeta+1
Medium·4 companies·Max freq 68%·Acc 59.8%
ShopeeAmazonMicrosoft+1
Medium·4 companies·Max freq 64%·Acc 65.6%
MetaTikTokAmazon+1
Medium·4 companies·Max freq 42%·Acc 66.8%
AdobeBloombergAmazon+1
Easy·4 companies·Max freq 25%·Acc 59.1%
GoogleMetaAmazon+1
Medium·4 companies·Max freq 25%·Acc 71.8%
AmazonMicrosoftMeta+1
Medium·3 companies·Max freq 78%·Acc 61.1%
Arista NetworksMetaGoogle
Hard·3 companies·Max freq 63%·Acc 61.2%
LinkedInGoogleAmazon
Medium·3 companies·Max freq 56%·Acc 88.4%
SAPAmazonGoogle
Easy·3 companies·Max freq 29%·Acc 59.5%
GoogleMetaAmazon
Hard·3 companies·Max freq 13%·Acc 39.6%
AmazonMicrosoftGoogle
Medium·2 companies·Max freq 100%·Acc 82.2%
CoupangAmazon
Medium·2 companies·Max freq 27%·Acc 45.9%
MicrosoftMeta
Medium·2 companies·Max freq 25%·Acc 80.3%
AmazonMeta
Easy·2 companies·Max freq 13%·Acc 79.0%
AmazonGoogle
Medium·1 companies·Max freq 25%·Acc 63.5%
Meta
Medium·1 companies·Max freq 25%·Acc 68.2%
Amazon
Medium·1 companies·Max freq 25%·Acc 44.7%
Google
Showing 37 of 37 problems in Binary Search Tree (BST)Filtered: All Companies