LearnThatStack Ace your next interview
Trees, BSTs & Heaps · question
Question 5 of 76

What is a binary search tree, and what invariant must every node satisfy?

beginner
← All Trees, BSTs & Heaps questions
Re-explain
Visual ↓

A binary search tree is a binary tree that keeps its values ordered for fast lookup. Its invariant is that for any node, all values in its left subtree are smaller and all values in its right subtree are larger.

The rule covers whole subtrees, not just direct children, so a value deep in the left subtree must stay below the ancestor it descends from.

The invariant makes search cheap. At each node you compare, then discard a whole subtree and follow the other side. Duplicates need a chosen policy, such as always going right or storing a count. Keep the invariant true on every insert and delete, and lookups stay logarithmic on a balanced tree. Break it anywhere and searches can miss values that are present.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Trees, BSTs & Heaps One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
The BST invariant covers whole subtrees
  • 8The root keeps smaller values left, larger right.
    • 3It is the left child of 8, so it must be smaller.
      • 1This left child of 3 is smaller than 3 and 8.
      • 6This right child of 3 is still smaller than 8.
        • 4This left child of 6 must be between 3 and 6.
        • 7This right child of 6 must stay between 6 and 8.
    • 10It is the right child of 8, so it must be larger.
      • 9This left child of 10 is still larger than 8.
      • 14This right child of 10 is larger than 10 and 8.

The rule covers whole subtrees. 6 is a right child, yet it is in the left subtree of 8, so it must still be smaller than 8.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. A binary search tree is a binary tree that keeps its values in order, so you can find a value without looking at every node.
  2. The invariant is that for any node, everything in its left subtree is smaller and everything in its right subtree is larger.
  3. That rule covers whole subtrees, not just direct children, so a node deep in the left subtree still has to be smaller than the ancestor above it.
  4. That's what makes search cheap. At each node you compare once, skip one whole subtree and follow the other side.
  5. Duplicates need a policy you choose up front, like always sending equal keys right or keeping a count on the node.
  6. The catch is that lookups are only logarithmic on a balanced tree, and if an insert or delete breaks the invariant, searches can miss values that are actually there.

Weak answers to avoid

  • Only compares a node with its children Left child < node < right child is a weaker rule. Say the invariant covers whole subtrees, or a deep node can end up on the wrong side of an ancestor.
  • Claims BST lookups are always O(log n) That holds only on a balanced tree. Sorted inserts build a chain of height n, so search is O(n). Say the cost is O(height).
  • Ignores duplicate keys Leaving equal keys undefined lets insert and search disagree. Name a policy, such as always going right or keeping a count.
  • Mixes up a BST with a heap A heap only orders each parent against its children, so search can't skip a subtree. A BST orders left against right, and that lets search discard one.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Trees, BSTs & Heaps cheatsheet.

← Back to all Trees, BSTs & Heaps questions
Pro · $10/mo

65 of 76 Trees, BSTs & Heaps answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime