# Binary Search Trees

> Use the BST ordering property for O(h) search, insert and delete, validate BSTs, and understand why balanced trees guarantee O(log n). Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-search-trees
Last updated: 2026-11-28

Use the BST ordering property for O(h) search, insert and delete, validate BSTs, and understand why balanced trees guarantee O(log n).

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Binary Trees and Traversals](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-trees)

A binary search tree keeps every value in the left subtree smaller than the node and every value in the right subtree larger. This makes search, insert and delete O(h), where h is the height.

### Key operations

- **Search / insert**: go left or right by comparison.
- **Delete**: three cases. A leaf is removed directly, a node with one child is replaced by it, and a node with two children is replaced by its **inorder successor** (smallest in the right subtree), which is then deleted.
- **Inorder traversal yields sorted order**, which solves k-th smallest, validate BST and two-sum-in-BST.
- **Floor / ceiling, successor / predecessor** by walking and remembering the last candidate.

### Balance

Inserting sorted data creates a linked-list-shaped tree with h = n. Self-balancing trees (**AVL**, **red-black**) rotate nodes to keep h = O(log n). You rarely implement them in interviews, but you should know that `std::map`, `TreeMap` and `std::set` are red-black trees, which is why their operations are O(log n).

### Pitfalls

Validating a BST by only comparing a node with its direct children is wrong; you must pass down a valid (min, max) range or check that the inorder sequence is strictly increasing. Watch for duplicates and integer overflow when using INT_MIN/INT_MAX as bounds (use long or null).

## Key concepts

- **BST property and search**
- **Insert and delete**
- **Validate BST with ranges**
- **Inorder as sorted order**
- **Floor, ceil, successor**
- **AVL and red-black trees (concepts)**
- **BST iterator**

## Mini project

Implement a BST class with insert, search, delete and inorder, then solve: Validate BST, Kth Smallest Element in a BST, LCA of a BST, Delete Node in a BST, Convert Sorted Array to BST, BST Iterator, and Two Sum IV.

## Check yourself

Why does checking only node.left.val < node.val < node.right.val at every node fail to validate a BST? Give a counterexample.

## Free resources

- [VisuAlgo: Binary Search Tree](https://visualgo.net/en/bst) (VisuAlgo)
- [Binary search tree](https://www.geeksforgeeks.org/binary-search-tree-data-structure/) (GeeksforGeeks)
- [Data Structure Visualizations (USFCA)](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html) (USFCA)

Previous: [Binary Trees and Traversals](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-trees) · Next: [Heaps and Priority Queues](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/heaps-priority-queues)

## More in Trees and Heaps

- [Binary Trees and Traversals](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-trees)
- [Binary Search Trees](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-search-trees)
- [Heaps and Priority Queues](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/heaps-priority-queues)
- [Tries (Prefix Trees)](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/tries)
- [Segment Trees and Fenwick Trees](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/segment-tree-fenwick)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
