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

How do you insert a new value into a binary search tree?

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

Insertion follows the path a search for the value would take, then attaches the new value where that search would have failed. You walk down comparing, going left for smaller and right for larger, until you reach an empty child slot. You place the new node there as a leaf.

New values always land as leaves, so existing nodes never move. That keeps insertion simple and preserves the ordering invariant, since you followed it on the way down.

Duplicates need a rule decided up front, like sending equals right or bumping a count on the existing node.

The cost mirrors search. It is proportional to height, so O(log n) on a balanced tree and O(n) on a skewed one. Sorted input builds a long one-sided chain that every operation walks like a linked list, which is why real systems self-balance after inserts.

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.
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. Inserting into a BST means searching for the value and putting it where that search would have failed.
  2. Starting at the root, I compare and go left if the new value is smaller, right if it's larger, until I hit an empty child slot.
  3. The new node goes into that slot as a leaf, so no existing node moves, and the ordering still holds because I followed it on the way down.
  4. I'd decide on duplicates up front, for example by sending equal values right or by keeping a count on the existing node.
  5. The cost depends on the tree's height, so it's O(log n) when the tree is balanced and O(n) when it's skewed.
  6. Sorted input builds a one-sided chain that every operation walks like a linked list, and that's why real systems use self-balancing trees.

Weak answers to avoid

  • Moves existing nodes to make room In a plain BST the new value always lands as a leaf and nothing moves. Rebalancing belongs to self-balancing trees, so keep the two ideas apart.
  • Claims insert is always O(log n) The cost depends on the tree's height. That's O(log n) only when the tree is balanced and O(n) when it's skewed, for example when built from sorted input. Give both cases.
  • No rule for duplicates Equal keys need a decision before you write any code. State your rule up front, such as sending equal keys right or keeping a count on the existing node.
  • Recursive insert that never relinks the child If the recursive call creates the node but the result never gets assigned back to left or right, the node is lost. Return the subtree root and reassign the child.
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