Skip to content

Tree traversal [mp]

Micro pattern

Description

Visit tree nodes in an order determined by when the root is processed relative to its children.

Problems

Traversal definitions

Choose preorder, inorder, or postorder according to when node work must occur.

Inorder

Traverse left, root, then right to process BST keys in ascending order.

Preorder

Process a root before recursively processing its subtrees.

Postorder

Compute child results before processing their parent.

Level traversal through next pointers

Use horizontal links to cross each level without a queue.