DSA Master › Topic 12
Binary Search Trees
In a BST, everything on the left is smaller than the node and everything on the right is larger. Two consequences matter most: an inorder traversal visits values in sorted order, and you can discard half the tree at each step.
Concepts
- BST properties
- Inorder traversal
- Search
- Insertion
- Deletion
How to recognise it
- You need the kth smallest value, or to check sortedness. Use inorder traversal.
- You're validating, searching, inserting or deleting in a BST.
- You need the LCA in a BST. Compare both values against the current node.
Common pitfalls
- Checking only parent vs child doesn't validate a BST. Pass min/max bounds down the tree.
- When deleting a node with two children, replace it with its inorder successor.
Problems
- Validate Binary Search Tree (Medium)
- Kth Smallest Element in a BST (Medium)
- Lowest Common Ancestor of a Binary Search Tree (Medium)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)