DSA · Patterns, not puzzles
What is a binary search tree?
Short answer
A binary search tree keeps every left descendant smaller than its node and every right descendant larger. Search, insert and delete are O(h), where h is the height and can degrade to O(n).
The ordering property lets you discard half the tree at each step, similar to binary search on an array. The catch is that the bound depends on height. A balanced BST has h = O(log n); a BST built from sorted input becomes a linked list with h = O(n).
function search(node, target) {
while (node !== null) {
if (node.value === target) return node;
node = target < node.value ? node.left : node.right;
}
return null;
}
function insert(node, value) {
if (node === null) return { value, left: null, right: null };
if (value < node.value) node.left = insert(node.left, value);
else if (value > node.value) node.right = insert(node.right, value);
return node;
}Why balance matters
- A balanced tree gives O(log n) search and insertion
- Sorted insertion order creates a skewed tree with O(n) operations
- Self-balancing trees such as AVL and red-black trees maintain the bound
- Inorder traversal of any valid BST returns sorted values
- Deletion has three cases: leaf, one child and two children
For the two-child deletion case, replace the node with its inorder successor, the smallest value in the right subtree, then delete that successor. This preserves the ordering property without restructuring the whole tree.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why can a BST degrade to O(n)?
- 2How do you delete a node with two children?
- 3How do AVL and red-black trees maintain balance?
- 4How would you validate whether a tree is a BST?
Say it in the interview
A binary search tree orders values so that everything left of a node is smaller and everything right is larger. Search and insertion follow that ordering and cost O(h), where h is the height. The weakness is that a skewed tree becomes a linked list, so real implementations use self-balancing variants. I also remember that the property applies to all descendants, not only the immediate children.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live DSA mock interview and get scored feedback on depth, clarity and confidence.