Learning path

Full curriculum

Full curriculum

Arrows go from each prerequisite to the units that depend on it. Hover or focus a unit to highlight its path.

Unit content

Tree traversal orders

A rooted tree can be processed recursively because each child is the root of a smaller subtree.

For a binary tree, three common depth-first orders differ only in when the current node is processed:

  • preorder: node, left subtree, right subtree;
  • inorder: left subtree, node, right subtree;
  • postorder: left subtree, right subtree, node.

The appropriate order depends on the task. Preorder naturally serializes a hierarchy from parent to descendants, inorder exposes sorted order in a binary search tree, and postorder is useful when a parent depends on results computed for its children.

These are tree-specific processing orders. General breadth-first search and depth-first search over arbitrary graphs are separate algorithms.