Practice Discrete Math

Algorithms / Tree Traversal Recursion

Least You Need to Know: Tree Traversals and Recursive Calls

Tree interview questions often hinge on **when** work happens relative to the recursive calls. Preorder acts before the children, inorder acts between them, and postorder acts after the children return.

The least you need to know

Key notation

preorder node before children
inorder left subtree, node, right subtree in a binary tree
postorder children before node

Tiny worked example

  • Suppose you need to print a binary tree in sorted order and the tree is a BST.
  • The BST property says every left value is smaller and every right value is larger.
  • So an inorder traversal prints all smaller values, then the root, then all larger values.
  • That is why inorder is the default sorted-output traversal for BSTs.

Common mistakes

How to recognize this kind of problem

Start practice