DSA · Patterns, not puzzles

What are the binary tree traversal orders?

Mediumbinary-treetraversalrecursionUpdated Oct 2026
Quiz this track

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.

js
function inorder(node, output = []) {
  if (node === null) return output;
  inorder(node.left, output);
  output.push(node.value);
  inorder(node.right, output);
  return output;
}
Inorder on a binary search tree returns values in ascending order.

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
js
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;
}
The iterative version avoids recursion depth limits by simulating the call stack explicitly.
All depth-first traversals are O(n) time and O(h) space, where h is the tree height. The space is O(log n) only for a balanced tree and O(n) for a skewed one.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Which traversal gives sorted output from a BST?
  2. 2How do you traverse a tree without recursion?
  3. 3What is the space complexity of a traversal?
  4. 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.