๐ŸŒฒ
๐ŸŒฒ Trees, Heaps & Tries ยท DS-201 ยท Session 3 of 6

Visiting Every Node: Traversals

Once you have a tree, you often need to visit every node โ€” but in what order? The four traversals (in-order, pre-order, post-order, level-order) each visit differently, and each is useful.

โฑ 8 min read๐Ÿ“… Updated 2026-08-09๐Ÿ“Š Tables, charts & examples

Having a tree is one thing; visiting its nodes is another - and unlike a list, there's more than one sensible order. The four classic traversals each visit every node but in a different order, and each is the right tool for different jobs: reading sorted data, copying a tree, evaluating expressions, or going level by level. This session covers the four traversals and when each is useful - a core skill for working with any tree.

The four traversals

Three traversals are depth-first (they go deep before wide), differing in when they visit a node relative to its children; the fourth is breadth-first (level by level).

TraversalOrderClassic use
In-orderleft, node, rightRead a BST's values sorted
Pre-ordernode, left, rightCopy/serialise a tree
Post-orderleft, right, nodeDelete a tree / evaluate expressions
Level-ordertop to bottom, level by levelBreadth-first search

The stack's <a href='/stack/larztree'>larztree</a> offers all four. The magic one for a BST is <b>in-order</b>: because left is always smaller, visiting left-then-node-then-right yields values in sorted order - which is why a BST reads back sorted. The others each shine elsewhere - pre-order for copying, post-order for cleanup or evaluating, level-order for going breadth-first.

Depth-first via recursion

The three depth-first traversals are beautifully simple with recursion: to traverse a node, you traverse its children - the only difference is where you 'visit' the node itself.

# in-order (yields sorted values for a BST):
#   traverse(left)
#   visit(node)        <- node visited between its children
#   traverse(right)
#
# pre-order: visit BEFORE children.  post-order: visit AFTER children.
# level-order uses a queue instead of recursion.
๐ŸŽฏ
The three depth-first traversals differ only in when you visit the node. Traverse the left child, traverse the right child - that recursion is the same for all three. The only difference is whether you 'visit' (record) the node before its children (pre-order), between them (in-order), or after them (post-order). That one small placement changes the order entirely. Level-order is different: it uses a queue to go breadth-first, level by level, exactly like the breadth-first search from graphs. Four ways to walk a tree, each a tool for a different job.

Walk a tree, four ways

  • In-order (left, node, right) - reads a BST sorted.
  • Pre-order (node, left, right) - copy/serialise a tree.
  • Post-order (left, right, node) - cleanup / evaluate expressions.
  • Level-order (breadth-first, via a queue) - go level by level.

๐Ÿงฉ Do it with LarzOS

Turn this lesson into action with the exact free tools and AI workflows for the job:

๐ŸŽฏ Key takeaways

  • Visiting a tree's nodes can be done in several orders - the four classic traversals.
  • In-order (left, node, right) yields a BST's values sorted; pre-order copies; post-order cleans up/evaluates; level-order goes breadth-first.
  • The three depth-first traversals differ only in WHEN you visit the node relative to its children (recursion does the rest).
  • Level-order uses a queue to go level by level, like breadth-first search - all four are in larztree.

Frequently asked

Why are there so many ways to traverse a tree - when would I use each?

Multiple traversal orders exist because different tasks naturally want to process a tree's nodes in different orders, and each traversal serves specific real purposes - so they're not arbitrary variety, but a toolkit where each order fits certain jobs. Let's go through when each is useful. In-order traversal (left, node, right) is the star for binary search trees: because a BST arranges smaller values left and larger right, visiting left-then-node-then-right produces the values in sorted order - so whenever you want to read a BST's contents sorted, in-order is the traversal, and it's why a BST can give you sorted data effortlessly. Pre-order traversal (node, then left, then right) visits a node before its children, which is ideal when you need to process a node before its subtrees - the classic use is copying or serialising a tree (recording a node before its children lets you reconstruct the tree in the same shape), and it's also natural for tasks like printing a directory structure with parents before their contents. Post-order traversal (left, right, then node) visits a node after its children, which suits tasks where you must handle children before their parent - the classic examples are deleting/freeing a tree (you delete children before the parent so you don't lose access to them) and evaluating expression trees (you compute the sub-results before combining them at the parent operator, since you need the operands before you can apply the operation). Level-order traversal (breadth-first, level by level from the top) visits nodes by depth, which is what you want when proximity to the root matters or you're exploring level by level - it's the tree version of breadth-first search, used for finding the shortest path in unweighted structures, processing a hierarchy level by level, or finding the closest matching node. So each traversal answers a different question about order: in-order for sorted processing (BSTs), pre-order for parent-before-children (copying, serialising), post-order for children-before-parent (deleting, evaluating), level-order for by-depth/breadth-first (shortest paths, level processing). You choose based on what your task requires about the order of visiting - and knowing all four means you can pick the right one rather than being stuck with a single order that doesn't fit. In practice, in-order (for sorted BST reading) and level-order (for breadth-first exploration) are the ones you'll reach for most, but pre-order and post-order have their essential niches (serialisation and cleanup/evaluation respectively). The variety reflects that trees are used for many purposes, and different purposes want different visiting orders - so the four traversals are a practical toolkit, each the natural fit for particular common tasks. The stack's larztree lets you try all four and see how the same tree yields different orders, which makes their differences concrete and memorable.

What's the difference between depth-first and breadth-first traversal?

Depth-first and breadth-first are two fundamentally different strategies for exploring a tree (or graph): depth-first goes deep along one path before backtracking, while breadth-first explores level by level, spreading out from the start before going deeper. Depth-first traversal dives down as far as it can along one branch before backing up to try others - it fully explores one subtree before moving to the next. The three depth-first tree traversals (in-order, pre-order, post-order) are all depth-first; they differ only in when they record the current node relative to recursing into its children, but all of them plunge deep into one side before the other. Depth-first is naturally expressed with recursion (each recursive call goes one level deeper) or with an explicit stack, and it's memory-efficient in the sense that it only needs to remember the current path from the root down. Breadth-first traversal (level-order for trees) instead visits all nodes at one depth before any nodes at the next depth - it processes the root, then all the root's children, then all their children, and so on, spreading outward level by level. Breadth-first is implemented with a queue: you start with the root in the queue, and repeatedly take a node out, process it, and add its children to the back of the queue - this naturally produces the level-by-level order. Breadth-first needs to remember all the nodes at the current 'frontier' (which can be many), so it can use more memory than depth-first for wide structures. When does the difference matter? Breadth-first is what you want when closeness to the start matters - notably, for finding the shortest path in an unweighted structure, because breadth-first reaches nearer nodes before farther ones, so the first time it reaches a target is via a shortest path. It's also natural for processing things by level or finding the nearest match. Depth-first is often simpler to write (recursion) and is what you want for tasks like fully exploring each branch, the specific ordered traversals (in/pre/post-order), detecting certain structural properties, or when you want to go deep. Both visit every node; the difference is the order and the exploration strategy - deep-first-then-backtrack versus level-by-level-outward - and the tools they use (stack/recursion for depth-first, queue for breadth-first). This distinction extends beyond trees to graphs, where depth-first search (DFS) and breadth-first search (BFS) are two of the most fundamental graph algorithms, used for different purposes (BFS for shortest unweighted paths, DFS for exploration, cycle detection, topological ordering, and more). So understanding depth-first versus breadth-first in trees prepares you for the same concepts in graphs. For trees specifically: the three classic traversals are depth-first (varying when the node is visited), and level-order is breadth-first (via a queue). Recognising which strategy a problem wants - go deep, or spread level by level - is a valuable skill, and the clearest single rule of thumb is that breadth-first (level-order) is the one to reach for when shortest-distance-from-the-start matters. The stack's larztree shows both: the recursive depth-first traversals and the queue-based level-order, so you can see the two strategies produce different orders on the same tree.

Session 3 of 6 complete

Next up: Heaps & Priority Queues

Continue to Session 4 โ†’

Related lessons

More in this course

Course home → · Browse all courses →