Inorder
L → Root → R
1,2,3,4,6,7,9Gives sorted output in BST
Preorder
Root → L → R
4,2,1,3,7,6,9Serialization, copy tree
Postorder
L → R → Root
1,3,2,6,9,7,4Delete tree, evaluate expr
Level Order
BFS level by level
4, 2 7, 1 3 6 9Shortest path, zigzag
Most tree problems follow this exact template. Fill in the logic at each node.
Watch how each value walks down the BST — compare at each node, go left if smaller, right if larger — until an empty slot is found. Highlighted (purple) nodes are the compare path; cyan shows the newly placed node.
Select a traversal order to see the visit sequence on the same 7-node BST. Green = already visited, cyan = currently visiting.
🧠 Under the Hood
A BST is stored as heap-allocated nodes linked by pointers — no contiguous array. Each node takes ~24 bytes (val + two pointers). A perfectly balanced BST of n nodes has height ⌊log₂ n⌋, giving O(log n) search/insert/delete. A skewed BST degrades to O(n). Standard library std::set / std::map use Red-Black trees to enforce O(log n) worst-case balance automatically.
⚖️ When to Use / When NOT to Use
std::set/std::map in interviews — they're RB-trees that guarantee balance automatically. Roll your own only if the problem requires custom structure.unordered_map/unordered_set — O(1) average lookup without the O(log n) overhead of a tree.std::set which is already balanced.🚫 Common Misconceptions
🎤 Interview Follow-ups
std::set) support this natively via lower_bound/upper_bound.