DSA · Patterns, not puzzles
What are the binary tree traversal orders?
Short answer
Inorder visits left, node, right; preorder visits node, left, right; postorder visits left, right, node; level order visits nodes by depth. Inorder on a binary search tree yields sorted values.
The three depth-first orders differ only in when the current node is processed. That single change produces very different sequences, so naming the order precisely matters. Level order is breadth-first and uses a queue instead of the call stack.
function inorder(node, output = []) {
if (node === null) return output;
inorder(node.left, output);
output.push(node.value);
inorder(node.right, output);
return output;
}The orders and their uses
- Preorder: copy a tree, serialize it, or explore roots before leaves
- Inorder: sorted output from a binary search tree
- Postorder: delete a tree, evaluate an expression tree, compute subtree results
- Level order: shortest path in an unweighted tree or breadth-first processing
function inorderIterative(root) {
const output = [];
const stack = [];
let node = root;
while (node !== null || stack.length > 0) {
while (node !== null) {
stack.push(node);
node = node.left;
}
node = stack.pop();
output.push(node.value);
node = node.right;
}
return output;
}Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Which traversal gives sorted output from a BST?
- 2How do you traverse a tree without recursion?
- 3What is the space complexity of a traversal?
- 4How would you serialize and deserialize a binary tree?
Say it in the interview
Inorder is left, node, right; preorder is node, left, right; postorder is left, right, node. I use inorder for sorted BST output, preorder for copying or serializing, and postorder when I need subtree results before the parent. Level order uses a queue. Iteratively I simulate the recursion with an explicit stack, which avoids call-stack limits.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live DSA mock interview and get scored feedback on depth, clarity and confidence.