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

Why does an in-order traversal of a BST produce sorted output?

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

In-order traversal visits the left subtree, then the node, then the right subtree, and the BST invariant puts those three parts in ascending order. Everything in a node's left subtree is smaller than the node, and everything in its right subtree is larger. So the traversal emits all smaller values before the node and all larger values after it. The same holds at every level, so the whole sequence comes out ascending.

inorder(node.left);
visit(node.val);
inorder(node.right);
// prints values low to high

This gives you a free sorted listing in O(n) without any extra sorting step. It also means you can spot a broken tree cheaply. If an in-order pass ever emits a value smaller than the previous one, some node violates the invariant.

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. In-order visits the left subtree, then the node, then the right subtree, and the BST invariant says that is exactly smallest to largest.
  2. Everything in a node's left subtree is smaller than the node, and everything in its right subtree is larger.
  3. So at any node, the walk emits all the smaller values first, then the node, then all the larger ones.
  4. The same argument holds at every level of the recursion, so the whole sequence comes out ascending, and you get a sorted listing in O(n) with no sorting step.
  5. The guarantee only holds if the invariant does, and that makes it a cheap check. If the walk ever emits a value smaller than the one before it, some node breaks the rule.

Weak answers to avoid

  • Answers 'because it's a BST' and stops That restates the question. Line up left, node, right against smaller, node, larger, then say why the same thing holds at every level.
  • Treats the invariant as parent-versus-child only Comparing a node with its direct children doesn't prove all left values come first. The rule covers whole subtrees, and the argument depends on that.
  • Mixes up in-order with pre-order or level-order Pre-order emits the node before its subtrees and level-order goes row by row. Only left-node-right matches the BST's ordering.
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