BST — inorder gives sorted order
WHAT is a BST? (precise foundation)
The key word in "BST" is Search — the ordering invariant exists precisely so we can binary-search through it.
WHY inorder produces sorted output
Derivation from scratch (proof by structural induction)
We never just assert it — we build the proof from the base case up.
Base case. An empty tree (or a single node) produces a list of length . A list of length 0 or 1 is trivially sorted. ✔
Inductive hypothesis (IH). Assume for any BST smaller than (i.e. its subtrees), inorder gives a sorted list.
Inductive step. Let have root , left subtree , right subtree .
- By IH, is sorted, and by the BST property every key in is . So:
- By IH, is sorted, and every key in is . So:
- Concatenate:
[ ...sorted, all < r ] + [r] + [ all > r, sorted... ]. The boundary between left list and is increasing (last ). The boundary between and right list is increasing ( first). Why this matters: a concatenation of two sorted lists is itself sorted iff the last of the first first of the second — and we just proved both joins respect that. ✔
By induction, inorder of any BST is sorted.

HOW to do it (code, two ways)
Worked trace
Tree:
8
/ \
3 10
/ \ \
1 6 14
- Dive left from 8 → 3 → 1. Print 1. 1 has no right.
- Pop 3, print 3. Go right to 6, dive left (none), print 6.
- Pop 8, print 8. Go right to 10, dive left (none), print 10, go right to 14, print 14.
- Output:
1, 3, 6, 8, 10, 14— sorted! Why? Each node printed only after its entire smaller-left family.
Complexity. Every node pushed/popped once → time. Space for the stack, where is height ( if balanced, worst-case skewed).
The reverse fact (Forecast-then-Verify)
Recall Verify
Yes — sorted inorder BST (with distinct keys). This gives a clean validation algorithm: do inorder, check each element previous. If any violation, it's not a BST. Cleaner than checking min/max ranges at every node.
Common mistakes (Steel-manned)
Flashcards
What property must every node satisfy in a BST?
What traversal order is "inorder"?
Why does inorder of a BST give sorted output?
What is the recursive equation for inorder of tree T?
What is the base case of the inorder-sorted proof?
Time and space complexity of inorder traversal?
How do you validate a BST using inorder?
Why is checking only immediate children insufficient for BST validation?
Is the converse true: sorted inorder implies BST?
What does the explicit stack in iterative inorder remember?
Recall Feynman: explain to a 12-year-old
Imagine kids lined up in a family tree where every kid is taller than everyone on their left and shorter than everyone on their right. You want to call them out shortest-to-tallest. So at each kid you say: "wait, let me first call everyone on your left side (they're shorter), then call YOU, then call everyone on your right (taller)." Do this for every kid and — magic — they come out in perfect height order. That left-me-right rule is all you need.
Connections
- Binary Search Tree — the structure whose invariant powers this.
- Tree Traversals — preorder/postorder/level-order (which do not sort).
- Validate BST — direct application of this fact.
- Binary Search — same "left smaller, right bigger" logic on arrays.
- Structural Induction — the proof technique used here.
- AVL & Red-Black Trees — keep so traversal stays fast.
- Kth Smallest Element in BST — stop inorder early at the kth output.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, BST (Binary Search Tree) ka rule simple hai: har node ke left mein chhote keys aur right mein bade keys hote hain — aur yeh rule recursively poore subtree pe lagta hai, sirf turant wale child pe nahi. Yahi cheez "Search" word ko meaning deti hai.
Ab inorder traversal ka order hai Left → Node → Right (L, N, R). Matlab kisi bhi node pe khade ho, pehle uske saare chhote (left) waale print karo, phir khud ko, phir saare bade (right). Jab har node pe yehi karte ho, toh output apne aap chhote se bade order mein aata hai — sorted! Isko hum induction se prove karte hain: empty/single node toh trivially sorted, aur agar left aur right subtree sorted hain aur root unke beech mein fit hota hai (last-left < root < first-right), toh poora list bhi sorted.
Ek bahut common galti: log sochte hain ki sirf left.key < node < right.key check karna kaafi hai BST validate karne ke liye. Yeh galat hai — deep node ancestor ka bound tod sakta hai. Best trick: inorder chalao aur check karo har element pichle se strictly bada hai ya nahi. Agar kahin nahi, toh BST nahi hai.
Yaad rakhne ka tareeka: "Inorder Rises" — L, N, R se values badhti hain. Time complexity , space height ke barabar. Yehi concept aage Kth smallest element aur Validate BST jaise problems mein direct use hota hai, isliye master kar lo.