Skip to content

FlashcardNot attempted

Tree traversals

Recall it before you reveal

Name the four tree traversals and what each is good for.

Space to reveal
Show answer & explanation
  • In-order (left, node, right): a BST in sorted order
  • Pre-order (node, left, right): copy or serialize a tree
  • Post-order (left, right, node): delete or free, compute heights/sizes bottom-up
  • Level-order (BFS with a queue): level by level, e.g. the shortest depth or rendering a tree by rows

DFS uses recursion or a stack (O(h) space); BFS uses a queue (O(width)).