Binary Search Tree BST Java | Fresher Interview
Fast recall
Quick revision
Core answer
h is the height - O(log n) when balanced, but O(n) if it degenerates into a linked list. An inorder traversal returns values sorted.Whether you state the recursive (whole-subtree) ordering rule and the balance-dependent complexity
Only checking immediate children instead of the whole subtree, or ignoring the O(n) degenerate case
Emphasize O(h): O(log n) balanced vs O(n) skewed, and inorder = sorted as the validity check
Interview-ready guide
Interview answer
A binary search tree is a binary tree with an ordering invariant.
For every node, all the values in its left subtree are smaller than the node, and all the values in its right subtree are larger. The crucial word there is every, and it applies to the whole subtree, not just the immediate left and right children.
A common mistake when validating a BST is to only compare a node with its direct children, which misses violations deeper down. The correct check passes a valid range down the recursion: each node must fall within a low and high bound, and you tighten that bound as you go left or right. The payoff of the ordering rule is efficiency.
Because I can decide at each node whether to go left or right, search, insert, and delete all run in O of h, where h is the height of the tree. When the tree is balanced, the height is about log n, so those operations are O of log n, which is the whole point of a BST.
But here's the catch I always raise: if I insert already-sorted data into a naive BST, every new value goes to one side, and the tree degenerates into what's essentially a linked list with height n, so operations become O of n. That's exactly why self-balancing trees like AVL and red-black trees exist, they keep the height logarithmic.
And one elegant property: an inorder traversal of a valid BST always produces the values in sorted order, which is both a neat consequence and a simple way to check validity.
Binary Search Tree BST Java | Fresher Interview
Direct Answer
A Binary Search Tree (BST) is a binary tree with an ordering rule: for every node, all values in its left subtree are smaller and all in its right subtree are larger. This makes search, insert, delete O(h) where h is the height - O(log n) when balanced, but O(n) if it degenerates into a linked list. An inorder traversal returns values sorted.
Code Example
class Node { int val; Node left, right; Node(int v){ val = v; } }
boolean search(Node n, int target) { // O(h)
while (n != null) {
if (target == n.val) return true;
n = (target < n.val) ? n.left : n.right; // go one side only
}
return false;
}
// Validity: every node must stay within (low, high) bounds
boolean isBST(Node n, long low, long high) {
if (n == null) return true;
if (n.val <= low || n.val >= high) return false; // whole-subtree rule
return isBST(n.left, low, n.val) && isBST(n.right, n.val, high);
}
// call: isBST(root, Long.MIN_VALUE, Long.MAX_VALUE)
Common Mistakes & Pitfalls
- Checking only direct children: validity needs the whole subtree within bounds - pass
(low, high)down. - Ignoring degeneration: sorted inserts make a naive BST O(n); mention balancing.
- Assuming O(log n) always: it's O(h); only balanced trees give log n.
- Forgetting inorder = sorted: the simplest validity check.
Check your understanding
Answer these without looking back at the article.
- How do you validate if a binary tree is a valid BST?
- What is a Red-Black Tree?
- When would you use TreeMap over HashMap?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.